- 博客(10)
- 收藏
- 关注
原创 约瑟夫函数版
def move(players,step):#将step的元素从列表中删除num = step - 1while num >0:tmp = players.pop(0)players.append(tmp)num = num-1return playersdef play(players,step,alive):#先生成一个列表,从[1,…,players]list1=[i for i in range(1,players+1)]#进入游戏的循环,每次数到step淘汰,step
2020-11-26 11:22:33
206
原创 2
#打开并读取文件file=open(r’C:\Users\Administrator\Desktop\o.xlsx’,‘r’)lines=file.readlines()#给每行添加行号,如#1#2#用#去对齐#最长行的长度max_len = 0for line in lines:if len(line)>max_len:max_len = len(line)print(max_len)line_num = 0new_lines = []for line in lines:
2020-11-19 12:16:36
76
原创 字符统计
file = open(r’C:\Users\Administrator\Desktop\Walden.txt’,‘r’)lines = file.readlines()words = []for line in lines:tmp_list = line.split(" ")for word in tmp_list:words.append(word.replace(’,’,’’).replace(’,’,’’).replace(’,’,’’).replace(’,’,’’).replace(
2020-11-19 11:34:00
107
原创 抽
file= open(r’C:\Users\Administrator\Desktop\o.xlsx’,‘r’)lines = file.readlines()#抽取每行的学号和姓名,保存到字典student={}for line in lines:tmp_list = line.spilt(’,’)xuehao = tmp_list[0]xingming = tmp_list[1]students[xuehao] = xingmingprint(students)#从学号中随机抽n个学
2020-11-19 10:15:26
86
原创 ...
import rewith open(‘romeo.txt’) as file:#1.1.读取txt文本文件内容file_txt=file.read()#2.文本预处理,去掉英文符号word_text=re.sub(r’[?.!,;""/[]]’,’ ‘,file_txt) #特殊字符替换成空格word_texts=re.sub(r"-",’ ',word_text) #替换单独的-,不是同一单词里的连字符#3.分割获得单词列表wordlist=word_texts.split()#4.获
2020-11-19 00:48:10
182
原创 猜
import randomm = random.randint(1, 100)total = 5while total > 0:n = int(input(‘一个1到100的整数:’))if n > m:print(‘大了’)total -= 1if total == 0:print(f’答案是 {n} ‘)else:print(f’还有 {total} 次机会’)elif n < m:print(‘小了’)total -= 1if total == 0:p
2020-11-11 23:36:16
59
原创 约瑟夫游戏
list2=[i for i in range(40)]del list2[0]print(list2)b=0a=0for i in range(1,99):if (len(list2)!=2):if(a0):b=(len(list2)%3+p)%3del list2[2:len(list2):3]print(list2)elif(a1):b=(len(list2)%3+p)%3del list2[1:len(list2):3]print(list2)elif(a==2):b
2020-11-11 23:30:07
100
原创 字典、元组、集合的使用
一、字典1.字典的定义2.总结:无序,存放多个值,可变 => 通过key取值3.字典的常见操作及方法:增删改查d1[key] = value: key存在就是改,不存在就是增;d1[key]: 取,如果key不存在就报错,所以用get(key, defalut);update({‘a’: 100, ‘c’: 300}): key有更新,无新增;pop(key): 根据key删且返回对应value;字典的取key或value的方法特别了解:dict是python中仅存的mapping类
2020-11-11 23:28:18
284
原创 list
all_in_list=[1,2,3,4,5,6]del all_in_list[0]del all_in_list[0]all_in_list.append (1)all_in_listdel all_in_list[0]del all_in_list[0]all_in_list.append (3)all_in_list
2020-11-04 22:12:09
152
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人