用于将列表转换为字典的Python程序工作不正常
它返回的字典如下所示:用于将列表转换为字典的Python程序工作不正常,python,dictionary,Python,Dictionary,它返回的字典如下所示: [['brisk', 'blow', 'wind', 'blow'], ['north', 'north', 'youth'], ['wind', 'cold', 'cold'], ['wind', 'yesteryear'], []] 出于某种原因,它似乎跳过了参数第二个列表中的“youth”条目,我不知道它为什么这样做 出于某种原因,For循环似乎跳过了这个词 这是我对重复的定义: {'blow': [1], 'north': [2], 'brisk': [1],
[['brisk', 'blow', 'wind', 'blow'], ['north', 'north', 'youth'], ['wind', 'cold', 'cold'], ['wind', 'yesteryear'], []]
出于某种原因,它似乎跳过了参数第二个列表中的“youth”条目,我不知道它为什么这样做
出于某种原因,For循环似乎跳过了这个词
这是我对重复的定义:
{'blow': [1], 'north': [2], 'brisk': [1], 'cold': [3], 'yesteryear': [4], 'wind': [1, 3, 4]}
我的程序的目标是从用户那里获取输入,清除所有的停止词等。
并打印出一个索引。列表中作为示例参数的每个列表都表示一行。因此,在这种情况下,dic_index()函数中的“lines”变量将是4。我认为您将
break
与pass
混淆了
尝试:
[编辑-解释]如果使用“break”,那么for
循环将在第一次复制时中断,并且该列表上剩余的所有内容都将被忽略。所以在[“北”,“北”,“青年”]中,第一个“北”是可以的,第二个“北”触发中断,循环甚至没有到达“青年”。
另一方面,如果使用“pass”,则忽略第二个“north”,并转到i+=1行
注:
我必须用
I
替换所有x
,用len(n)
替换leng
来修复您的程序。您的代码使用break
结束每行循环,跳过行中重复单词后面的任何单词。您可能想改用continue
。然而,您的代码是不必要的复杂
使用enumerate()
对行进行编号,使用collections.defaultdict
方便,使用set()
跟踪已计数的单词:
if duplicate(word, words, i+1)==True:
pass
演示:
>>从集合导入defaultdict
>>>样本=['brisk','blow','wind','blow'],['north','north','youth'],['wind','cold','cold'],['wind','yesteryear'],[]
>>>def dic_索引(行):
... 索引=默认目录(列表)
... 对于i,枚举中的行(第1行):
... seen=set()
... 对于行中的单词:
... 如果看到单词:
... 持续
... 见。添加(word)
... 索引[word]。追加(i)
... 回报指数
...
>>>dic_指数(样本)
defaultdict(,{'blow':[1],'north':[2],'brisk':[1],'youth':[2],'cold':[3],'yesteryear':[4],'wind':[1,3,4])
行和x
从何而来?重复()的定义是什么??不要把它当作一个问题,解释一下你在这里试图解决的实际问题。你希望输出是什么?你的代码创建了一个无限循环;你从不增加i
。每次我发布答案,你发布你的答案,我都觉得你是我的私人Python培训师。谢谢:poh,对-pass
在这段特定的代码中不会给您带来麻烦,但是您应该使用@MartijnPieters正确编写的continue
。
def duplicate(word, dic, line):
if word not in dic:
return False
values = dic[word]
length = len(values)
if values[length-1] == line:
return True
else:
return False
if duplicate(word, words, i+1)==True:
pass
from collections import defaultdict
def dic_index(lines):
indices = defaultdict(list)
for i, line in enumerate(lines, 1):
seen = set()
for word in line:
if word in seen:
continue
seen.add(word)
indices[word].append(i)
return indices
>>> from collections import defaultdict
>>> sample = [['brisk', 'blow', 'wind', 'blow'], ['north', 'north', 'youth'], ['wind', 'cold', 'cold'], ['wind', 'yesteryear'], []]
>>> def dic_index(lines):
... indices = defaultdict(list)
... for i, line in enumerate(lines, 1):
... seen = set()
... for word in line:
... if word in seen:
... continue
... seen.add(word)
... indices[word].append(i)
... return indices
...
>>> dic_index(sample)
defaultdict(<type 'list'>, {'blow': [1], 'north': [2], 'brisk': [1], 'youth': [2], 'cold': [3], 'yesteryear': [4], 'wind': [1, 3, 4]})