# 输入整数,输出百位及以上的数字
# s=input("输入整数")
# print(s[:-2])
#
# 获取输入字符串,按照空格分割,按行打印
# i=input("输入字符串")
# s=i.split()
# for c in s:
# print(c)
#
# 输入数字1--7,输出星期几
# d={1:"monday",2:"tuesday",3:"wednesday",4:"thursday",5:"friday",6:"saturday",7:"sunday"}
# i=int(input("""please input a number between 1--7
# """))
# a=d.get(i,"错误")
# print(a)
#
# 回文数,正反排列相同的数
# s=input("输入一个5位自然数")
# if s[0]==s[-1] and s[1]==s[-2]:
# print('回文')
# else:
# print('不是回文')
#
# 输入一个十进制数,输出二进制、八进制、十六进制
# s=int(input("a number:"))
# print("二进制:{:b}".format(s))
# print("八进制:{:o}".format(s))
# print("十六进制:{:x}".format(s))
#判断是否为闰年
# i=int(input("输入年份"))
# if(i%4==0 and i%100 !=0)or i%400==0:
# print(i,'闰年')
# else:
# print("不是闰年")
#最大公约数计算
# a,b=map(int,input("输入两个数,逗号分隔:").split(","))
# x=a*b
# if a>b:
# while True:
# s=a%b
# if s==0:
# print("最大公约数:",b)
# print("最小公倍数:",int(x/b))
# break
# a,b=b,s
# elif a<b:
# while True:
# s=b%a
# if s==0:
# print("最大公约数:",a)
# print("最小公倍数:",int(x/a))
# break
# a,b=s,a
# else:
# print("最大公约数:",a)#两数相等
# print("最小公倍数:",int(x/a))
#统计不同字符的个数
# z,e,k,q=0,0,0,0
# str=input("输入字符:")
# for i in str:
# if i.isalpha():
# e+=1
# elif i.isdigit():
# z+=1
# elif i==" ":
# k+=1
# else:
# q+=1
# print("文字{},数字{},空格{},其他{}".format(e,z,k,q))
#猜数游戏续
# import random
# ran=random.randint(1,1000)
# count=0;
# while True:
# try:
# i=eval(input("输入一个整数"))
# except:
# print("类型错误,请输入整数")
# continue
# if type(i)!=type(1):
# print("类型错误,重新输入")
# continue
# else:
# count+=1
# if i>ran:
# print("大了,{}次".format(count))
# elif i<ran:
# print("小了,{}次".format(count))
# else:
# print("对了,{}次".format(count))
# break
#羊车门问题
import random
j=1
count_n=0 #不更换选择猜到的次数
count=0 #更换猜到的次数
while j<=100000:
j+=1
i=random.randint(0,2) #随机猜一个数字,0车,1、2羊
#如果猜对了车则count_n+1,如果猜到羊count+1
if i==0:
count_n+=1
else:
count+=1
print("不换获胜的概率:",count_n/100000)
print("交换获胜的概率:",count/100000)
#isNum()函数
# def isNum(s):
# if type(s)==type(1+1j) or type(s)==type(1) or type(s)==type(1.1):
# return 1
# else:
# return 0
#isPrime函数,参数为整数,异常处理
# def isPrime(n):
# try:
# n=int(n) #如果字符串的浮点数强转int类型会报错
# t=0
# for i in range(1,n+1):
# if n % i !=0:
# continue
# else:
# t+=1
# if t==2:
# return True
# else:
# return False
# except:
# return "请输入整数"
#
# x=input("请输入整数")
# print(isPrime(x))
#两百以内的素数
def print_prime():
for i in range(1,201):
c=0
if i==1:
print(1,end=" ")
else:
for j in range(2,i+1):
if i%j==0:
c+=1
if c<=1:
print(i,end=" ")
print_prime()
#斐波那契数列
# def feibonaqi(n):
# a,b=1,1
# i=1
# while i<n:
# b,a=a+b,b
# i+=1
# print(a)
# feibonaqi(2)
#英文字符频率统计,降序输出
# s=input("请输入一串字符")
# s=s.lower()
# counts={}
# for character in s:
# if 97<=ord(character)<=122:
# counts[character]=counts.get(character,0)+1
# else:
# continue
# characters=list(counts.items())
# sorted_characters=sorted(characters,key=lambda x:x[1],reverse=True)
# print(type(sorted_characters))
# for i in range(len(sorted_characters)):
# cha,count=sorted_characters[i]
# print("{:<10}{:>10}".format(cha,count))
#中文字符统计,降序输出
# n=input("请输入一段字符")
# counts={}
# for character in n:
# counts[character]=counts.get(character,0)+1
# characters=list(counts.items())
# sort_character=sorted(characters,key=lambda x:x[1],reverse=True)
# for i in range(len(sort_character)):
# cha,count=sort_character[i]
# print("{:<10}{:>10}".format(cha,count))
#随机在26个字母和9个数字里生成10个8位密码
# import string
# from random import *
# password=[]
# resource=string.ascii_letters+string.digits #数字和字母
# for count in range(10):
# password=[]
# for num in range(8):
# i=randint(0,len(resource)-1)
# password.append(resource[i])
# else:
# print("".join(password))
# password.clear()
#重复元素判定
# def judge(n):
# m=sorted(n)
# for i in range(0,len(m)-1):
# if m[i]==m[i+1]:
# return True
# else:
# return False
# line=list(input("请输入一串列表值"))
# print(judge(line))
#重复元素判定续
def judge(n):
m=set(n)
if len(m)!=len(n):
return True
else:
return False
line=list(input("请输入一串列表值"))
print(judge(line))
#统计字符在文件中的次数
# filename=input("请输入文件名:")
# str=input("请输入一个字符:")
# f=open(filename,"r")
# all=f.read()
# number=all.count(str)
# f.close()
# print("字符{}在文件{}中出现的次数是{}次".format(str,filename,number))
#读取英文文件,大写字母变小,小写变大
# filename=input("请输入英文文本文件名:")
# f=open(filename,"r")
# s=f.read()
# #方法一
# result=s.swapcase() #swapcase()用于大小写字母变换
# print(result)
#生成10x10的矩阵保存为csv文件
from random import *
s=""
for y in range(10):
line=""
for x in range(10):
line+="{} ".format(randint(0,9))
s+="{}\n".format(line)
#将矩阵保存为文件
file=open("F:\\Q7.txt","w")
file.write(s)
file.close()
#将矩阵另存为csv文件
CSV=open("F:\\Q7.txt","w")
newfile=s.replace(" ",",")
CSV.write(newfile)
CSV.close()
#读取python源文件,除保留字的字母外的小写字母变成大写,生成后的文件能被执行
import keyword
table=["range","print","input","len","list","set"]
file=open("test1.py","r",encoding='utf-8')
content=file.read()
file.close()
temp="" #临时字符串,用于判断和处理
fs="" #用于存储改变后的内容
for ch in content:
if ch.isalpha(): #如果是字母,则加到temp后面,用于后面处理
temp+=ch
else: #如果不是字母,表示一个连续的字母字符串结束,此时ch非字母
if(not keyword.iskeyword(temp))and (temp not in table):
temp=temp.upper() #如果不是保留字和内置方法,将其变成大写
fs+=temp #将temp添加到fs
fs+=ch #添加非字母,不能和上一个颠倒
temp="" #清空临时字符串,用于下次使用
new_file=open("test1.py","w",encoding='utf-8')
new_file.write(fs)
new_file.close()
#将任意类型元素的列表转储为CSV
#处理列表的函数
def save_csv(m,n):
CSV=open(m,"w")
t=""
for i in range(len(n)):
t+=str(n[i])+"列表分隔处"
t=t.replace(",","此处为原来的逗号")
x=list(filter(None,t.split("列表分隔处"))) #filter有两个参数,第一个参数是函数,第二个是传递给函数的参数,返回True时放到新列表里
CSV.write(",".join(x)) #None只返回True
CSV.close()
#打开CSV文件并正确解析为列表的函数
def open_csv(m):
file=open(m,"w").read()
file=file.replace(",","从此处分隔")
file=file.replace("此处为原来的逗号",",")
new_list=file.split("从此处分隔")
print("解析出的列表为{}".format(new_list))
def main():
n=input("请输入列表元素,以分号分隔")
m=input("请输入要保存的csv文件名")
n=n.split(";")
print("输入的列表为{}".format(n))
save_csv(m,n)
open_csv(m)