python集合翻转教学 - 大白社区
- 发帖时间:
- 2023-05-07 17:57:32
摘要:集合翻转教学 第1关:统计小说单词数量;第2关统计小说中不重复单词数量;第3关列表去掉重复元素后保持各元素出现的先后次序不变;第4关输出两本小说中共存的单词数量;第5关输出两本小说中出现的所有单词数量;第6关统计仅在第一本小说中出现的单词数量;第7关统计未同时在两本小说中出现的单词数量
目录
第1关:统计小说单词数量
本关任务:编写一个统计英文小说中单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
预期输出:
27154
import string
def file_to_str(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写,返回字符串"""
# 补充你的代码
with open(file, 'r', encoding='utf-8') as f: # 创建文件对象
txt = f.read() # 读取文件为一个字符串
return txt.lower() # 返回字符串,其中字母全部转为小写
def file_to_lst(txt):
"""替换掉字符串txt中的符号和数字,根据空格切分为列表,返回列表"""
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls # 返回列表
if __name__ == '__main__':
filename = input() # 输入文件名
path = '/data/bigfiles/' # 文件存放路径
text = file_to_str(path + filename) # 读文件返回字符串
words_lst = file_to_lst(text) # 字符串切分为列表
print(len(words_lst)) # 输出列表长度
第2关 统计小说中不重复单词数量
本关任务:编写一个统计英文小说中不重复单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
预期输出:
2557
import string
def file_to_str(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写"""
# 补充你的代码
with open(file, 'r', encoding='utf-8') as f: # 创建文件对象
txt = f.read() # 读取文件为一个字符串
return txt.lower() # 返回字符串,其中字母全部转为小写
def file_to_lst(txt):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写,
替换掉其中的符号和数字,根据空格切分为列表,返回列表"""
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls # 返回列表
def no_repeat(words_ls):
"""接收列表为参数,去除里面的重复单词,保持原来单词出现的顺序,返回列表"""
# 补充你的代码
words_no_repeat = set(words_ls) # 去掉重复单词,返回值为集合
return words_no_repeat # 返回集合
if __name__ == '__main__':
filename = input() # 输入文件名
path = '/data/bigfiles/' # 文件存放路径
text = file_to_str(path + filename) # 读文件返回字符串
words_lst = file_to_lst(text) # 字符串切分为列表
print(len(no_repeat(words_lst))) # 输出集合长度
第3关 列表去掉重复元素后保持各元素出现的先后次序不变
本关任务:编写一个能将小说中重复单词去掉再按出现次序输出的小程序。
测试输入:
The Old Man and the Sea.txt
10
预期输出:
['a', 'distributed', 'proofreaders', 'canada', 'ebook', 'this', 'is', 'made', 'available', 'at']
import string
def file_to_str(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写"""
# 补充你的代码
with open(file, 'r', encoding='utf-8') as f: # 创建文件对象
txt = f.read() # 读取文件为一个字符串
return txt.lower() # 返回字符串,其中字母全部转为小写
def file_to_lst(txt):
"""替换掉字符串中的符号和数字,根据空白字符切分为列表,返回列表"""
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls # 返回列表
def no_repeat(words_ls):
"""接收列表为参数,去除里面的重复单词,保持原来单词出现的顺序,返回列表"""
# 补充你的代码
words_no_repeat = list(set(words_ls)) # 去掉重复单词,返回值为列表
return sorted(words_no_repeat, key=lambda x: words_ls.index(x)) # 返回列表 根据元素在列表中出现的序号排序
if __name__ == '__main__':
filename = input() # 输入文件名
n = int(input()) # 输入一个正整数n
path = '/data/bigfiles/' # 文件存放路径
text = file_to_str(path + filename) # 读文件返回字符串
words_lst = file_to_lst(text) # 字符串切分为列表
print(no_repeat(words_lst)[:n]) # 输出不重复的前n个单词的子列表
第4关 输出两本小说中共存的单词数量
本关任务:编写一个统计两本小说中共存的单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
The Torrents of Spring.txt
预期输出:
1127
import string
def file_to_set(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写。
替换掉字符串中的符号,根据空白字符切分为列表,转为集合类型。"""
path = '/data/bigfiles/' # 文件路径
with open(path+file, 'r', encoding='utf-8') as fr: # 创建文件对象
txt = fr.read().lower() # 读取文件为一个字符串,其中字母全部转为小写
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls
def words_both(file1, file2):
"""接收两个文件名为参数,返回两个文件中共同存在的单词,相同单词只计算一次"""
# 补充你的代码
file1 = {i for i in file_to_set(file1)}
file2 = {i for i in file_to_set(file2)}
file3 = file1 & file2
return file3
if __name__ == '__main__':
filename1 = input() # 输入文件名
filename2 = input() # 输入文件名
print(len(words_both(filename1, filename2)))
第5关 输出两本小说中出现的所有单词数量
本关任务:编写一个统计两本小说中出现的所有单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
The Torrents of Spring.txt
预期输出:
4723
import string
def file_to_set(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写。
替换掉字符串中的符号,根据空白字符切分为列表,转为集合类型。"""
path = '/data/bigfiles/' # 文件路径
with open(path+file, 'r', encoding='utf-8') as fr: # 创建文件对象
txt = fr.read().lower() # 读取文件为一个字符串,其中字母全部转为小写
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls # 返回列表
def words_all(file1, file2):
"""接收两个文件名为参数,返回两个文件中出现的所有单词,相同单词只计算一次"""
# 补充你的代码
file1 = {i for i in file_to_set(file1)}
file2 = {i for i in file_to_set(file2)}
file3 = file1 | file2
return file3
if __name__ == '__main__':
filename1 = input() # 输入文件名
filename2 = input() # 输入文件名
print(len(words_all(filename1, filename2)))
第6关 统计仅在第一本小说中出现的单词数量
本关任务:编写一个统计仅在第一本小说中出现的单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
The Torrents of Spring.txt
预期输出:
1429
import string
def file_to_set(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写。
替换掉字符串中的符号,根据空白字符切分为列表,转为集合类型。"""
path = '/data/bigfiles/' # 文件路径
with open(path+file, 'r', encoding='utf-8') as fr: # 创建文件对象
txt = fr.read().lower() # 读取文件为一个字符串,其中字母全部转为小写
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls
def only_in_first(file1, file2):
"""接收两个文件名为参数,返回仅在第一本小说中出现且在第二本小说中未出现的单词集合,相同单词只计算一次"""
# 补充你的代码
file1 = {i for i in file_to_set(file1)}
file2 = {i for i in file_to_set(file2)}
file3 = file1 - file2
return file3
if __name__ == '__main__':
filename1 = input() # 输入文件名
filename2 = input() # 输入文件名
print(len(only_in_first(filename1, filename2)))
第7关 统计未同时在两本小说中出现的单词数量
本关任务:编写一个统计两本小说中出现的单词中未同时在两本书中出现的单词数量的小程序。
测试输入:
The Old Man and the Sea.txt
The Torrents of Spring.txt
预期输出:
3596
import string
def file_to_set(file):
"""将文件名变量file指向的文件读为字符串,全部字母转为小写。
替换掉字符串中的符号,根据空白字符切分为列表,转为集合类型。"""
path = '/data/bigfiles/' # 文件路径
with open(path+file, 'r', encoding='utf-8') as fr: # 创建文件对象
txt = fr.read().lower() # 读取文件为一个字符串,其中字母全部转为小写
# 补充你的代码
for c in string.punctuation: # 遍历符号集
txt = txt.replace(c, ' ') # 将全部符号都替换为空格
words_ls = txt.split() # 根据空白字符切分为列表
return words_ls
def only_in_one(file1, file2):
"""接收两个文件名为参数,返回仅在一个小说中存在,不在两个文件中共同存在的单词,相同单词只计算一次"""
# 补充你的代码
file1 = {i for i in file_to_set(file1)}
file2 = {i for i in file_to_set(file2)}
file3 = file1 ^ file2
return file3
if __name__ == '__main__':
filename1 = input() # 输入文件名
filename2 = input() # 输入文件名
print(len(only_in_one(filename1, filename2)))