这是一个程序,将单词(文本)列表中的数字添加到数字列表(称为数字)中,以表示原始文本的索引,例如,“水手出海看他能看到什么,但他所能看到的只有深海海底”这句话应该返回为"1 2 3 4 5 5 4 4 6 6 7 9 6 6 6 11 12 9 6 6 6 13 1 14 15 17 5 5 5“,但返回为"1 2 3 4 4”。5 5 5 4 9 10 11 12 9 9 13 14 15 12 9 9 16 1 17 18 19 20 5 5引起麻烦。
这是计划的一部分,也是问题所在:
for position, item in enumerate(text):
if text.count(item) < 2:
numbers.append(max(numbers) + 1)
else:
numbers.append(text.index(item) + 1)
“数字”和“文本”都是列表。
发布于 2017-01-27 20:20:47
一个简单的解决方案是在没有重复的情况下获得文本的创建版本,但保持相同的顺序,并使用index()
从原始文本中查找单词的索引。
通过按空格拆分从字符串中创建列表:
text="the sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea"
listText=text.split(" ")
创建不包含文本中所有单词的重复项的新列表,使用count()
检查word之前没有出现:
unique_text=[listText[x] for x in range(len(listText))if listText[:x].count(listText[x])<1]
使用列表理解来获取listText
中unique_text中每个单词的索引(并添加1):
positions=[unique_text.index(x)+1 for x in listText]
最终代码:
text="the sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea"
listText=text.split(" ")
unique_text=[listText[x] for x in range(len(listText))if listText[:x].count(listText[x])<1]
positions=[unique_text.index(x)+1 for x in listText]
输出:
[1, 2, 3, 4, 5, 5, 5, 4, 6, 7, 8, 9, 6, 6, 6, 10, 11, 12, 8, 9, 6, 6, 6, 13, 1, 14, 15, 1, 16, 17, 5, 5, 5]
发布于 2017-01-27 19:55:22
有字典的解决方案:
text="the sailor went to sea sea sea to see what he could see see see but all that he could see see see was the bottom of the deep blue sea sea sea"
l=text.split(' ')
d=dict()
cnt=0
for word in l :
if word not in d :
cnt+=1
d[word]=cnt
out=[d[w] for w in l]
#[1, 2, 3, 4, 5, 5, 5, 4, 6, 7, 8, 9, 6, 6, 6, 10, 11, 12, 8, 9, 6, 6, 6, 13, 1, 14, 15, 1, 16, 17, 5, 5, 5]
https://stackoverflow.com/questions/41901854
复制相似问题