目录
第1关 列表的属性与方法
第2关 推导式与生成器
第3关 列表的合并与排序
第4关 二维列表排序
第5关 动物重量排序
第6关 身份证号升位
第7关 完美立方数
第8关 约瑟夫环问题
第9关 文本分析(2)——统计英文文件中的单词数
list = []
n = int(input())
for i in range(n):
a = input().split()
if a[0] == 'insert':
list.insert(int(a[1]),int(a[2]))
elif a[0] =='append':
list.append(int(a[1]))
elif a[0] =='remove':
list.remove(int(a[1]))
elif a[0] =='sort':
list.sort()
elif a[0] =='pop':
list.pop()
elif a[0] =='reverse':
list = list[::-1]
elif a[0] =='print':
print(list)
ls = ['the lord of the rings','anaconda','legally blonde','gone with the wind']
s = input() # 输入一个字符
# 当输入为"1"时,输出元素为0-9的3次方的列表 [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
if s == '1':
print([x**3 for x in range(10)])
# 当输入为"2"时,输出元素为0-9中偶数的3次方的列表 [0, 8, 64, 216, 512]
elif s == '2':
list =[]
for i in range(10):
if i%2==0:
x= i**3
list.append(x)
print(list)
# 当输入为"3"时,输出元素为元组的列表,元组中元素依次是0-9中的奇数和该数的3次方[(1, 1), (3, 27), (5, 125), (7, 343), (9, 729)]
elif s == '3':
list =[]
for i in range(10):
if i%2!=0:
x=tuple((i,i**3))
list.append(x)
print(list)
# 当输入为"4"时,将ls中每个元素单词首字母大写输出['The lord of the rings', 'Anaconda', 'Legally blonde', 'Gone with the wind']
elif s == '4':
print([list.strip().capitalize() for list in ls])
# 当输入为其他字符时,执行以下语句
else:
print('结束程序')
list1 = list(map(int,input().split()))
list2 = list(map(int,input().split()))
list3 = list1 + list2
list3.sort(reverse = True)
print(list3)
list1 = [('dungeon',7),('winterfell',4),('bran',9),('meelo',6)]
list2 = [['Angle', '0121701100106',99], ['Jack', '0121701100107',86], ['Tom', '0121701100109',65], ['Smith', '0121701100111', 100], ['Bob', '0121701100115',77], ['Lily', '0121701100117', 59]]
list3 = sorted(list1,key = lambda x: x[1])
list4 = sorted(list2,key = lambda x: x[0])
list5 = sorted(list2,key = lambda x: x[2])
m = int(input())
n = int(input())
print(list3[:m])
print(list4[:n])
print(list5[:n])
list1 = []
while True:
a = input().split()
if len(a) == 0:
break
else:
list1.append(a)
list2 = sorted(list1,key = lambda x: float(x[1][:-1])*1000 if x[1][-1]=="t" else float(x[1][:-2]))
print(list2)
n = input()
list1 = list(n)
Wi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
if int(list1[6])+int(list1[7]) >= 5:
list1.insert(6,9)
list1.insert(6,1)
else:
list1.insert(6,0)
list1.insert(6,2)
s = 0
for i in range(17):
s += int(list1[i]) * int(Wi[i])
m = s % 11
list2 = [1,0,'X',9,8,7,6,5,4,3,2]
list1.append(list2[m])
for x in list1:
print(*str(x),end = '')
N = int(input())
for a in range(2,N+1):
for b in range(2,a):
for c in range(b,a):
for d in range(c,a):
if a**3 == b**3+c**3+d**3:
print(f'Cube = {a},Triple = ({b},{c},{d})')
n,k =map(int,input().split())
ls1 = [i for i in range(1,n+1)]
num = 0
if k < 2 or n < k :
print('Data Error!')
else:
while len(ls1) >= k:
num +=1
count = ls1.pop(0)
if num % k != 0:
ls1.append(count)
print(ls1)
def read_file(file):
"""接收文件名为参数,读取文件中的数据到字符串中,返回这个字符串"""
with open(file, 'r', encoding='utf-8') as text: # 创建文件对象
txt =text.read() # 读文件为字符串
return txt # 返回字符串
def word_list(txt):
"""接收字符串为参数,用空格替换字符串中所有标点符号,根据空格将字符串切分为列表
返回值为元素为单词的列表"""
for i in ",.!\'":
txt = txt.replace(i, ' ')
return txt.split()
def number_of_words(ls):
"""接收一个以单词为元素的列表为参数,返回列表中单词数量,返回值为整型"""
return len(ls)
if __name__ == '__main__':
filename = input() # 读入文件名
text = read_file('step10/'+filename) # 读取'step10/'文件夹中用户输入的文件名得到文件内容,存入text
words_list = word_list(text) # 处理text,得到单词的列表
words_counts = number_of_words(words_list) #统计单词列表word_list里的单词数
print(f'共有{words_counts}个单词')