题目:给定如下一段英文
A major drawback of cross-network recommender solutions is that they
can only be applied to users that are overlapped across networks.
Thus, the non-overlapped users, which form the majority of users are
ignored. As a solution, we propose CnGAN, a novel multi-task learning
based recommend architecture。
编写一个函数,要求实现以下功能:1)统计有多少个不同的单词;2)根据每
个单词 ASCII 码值的和(单词 they ASCII 码值的和是:
116+104+101+121=442)对单词进行从小到大的排序,重复出现的单词只算一次
的和,按行输出单词及对应的和。
思路分析:
对英文句子,自定义一个分词函数,将句子切分成为单词,并且写入 list,
list 转换为 set 进行去重,定义 Sum 函数对 list 中的单词利用函数 order()
进行求出 ASCII 码值,写入列表 m ,再定义 Sort 函数将 list 与 m 连接成为字
典函数,再对字典利用可以值进行排序。
实验步骤:
1 分词函数 Qie§
def Qie(p):
words = [] # 建立一个空列表
index = 0 # 遍历所有的字符
start = 0 # 记录每个单词的开始位置
while index < len(p): # 当 index 小于 p 的长度
start = index # start 来记录位置
while p[index] != " " and p[index] not in[".", ","]: # 若不
是空格,点号,逗号
index += 1 # index 加一
if index == len(p): # 若遍历完成
break # 结束
words.append(p[start:index])
if index == len(p):
break
# 判断如果是空格,或者是 . ,都要加上一,但是不写入 words
while p[index] == " " or p[index] in [".", ","]:
index += 1
if index ==len(p):
break
# 将 list 转换为 set 利用 set 的性质 进行去重