python无法获得字典的正确输出

python无法获得字典的正确输出,python,python-3.x,Python,Python 3.x,该文件包含以下字符串: I have no pride I have no shame You gotta make it rain Make it rain rain rain 输出应如下所示: {'rain': [2, 3], 'gotta': [2], 'make': [2], 'it': [2, 3], 'shame': [1], 'I': [0, 1], 'You': [2], 'have': [0, 1], 'no': [0, 1], 'Make': [3

该文件包含以下字符串:

I have no pride     
I have no shame   
You gotta make it rain    
Make it rain rain rain
输出应如下所示:

 {'rain': [2, 3], 'gotta': [2], 'make': [2], 'it': [2, 3], 'shame': [1], 'I': [0, 1], 'You': [2], 'have': [0, 1], 'no': [0, 1], 'Make': [3], 'pride': [0]}
但我明白了:

{'I': 1, 'have': 1, 'gotta': 2, 'Make': 3, 'it': 3, 'rain': 3, 'You':
 2, 'no': 1, 'make': 2, 'shame': 1, 'pride': 0}
我的代码:

def lineIndex(fName):
    fileName=open(fName)
    contents=fileName.readlines()
    fileName.close()
    d={}
    lst=[]
    count=-1
    for line in  contents:
        if line not in lst:
            print(line)
            lst.append(line)
            count+=1

        t=line.split()
        y2=[]    
        for eachWord in t:
            #print(eachWord)
            if eachWord not in d:
                y2.append(eachWord)
                d[eachWord]=count
            if eachWord in d:
                d[eachWord]=count

    return d
问题在于:

y2=[]
for eachWord in t:
    #print(eachWord)
    if eachWord not in d:
        y2.append(eachWord)
        d[eachWord]=count
    if eachWord in d:
        d[eachWord]=count
您不断将每个键的值重置为最新的行号。相反,请尝试使用
collections.defaultdict
使每个值默认以列表开头,并枚举行以获得计数:

import collections

def lineIndex(fName):
    d = collections.defaultdict(list)
    with open(fName) as f:
        for idx,line in enumerate(f):
            for word in set(line.split()):
                d[word].append(idx)
    return d
问题在于:

y2=[]
for eachWord in t:
    #print(eachWord)
    if eachWord not in d:
        y2.append(eachWord)
        d[eachWord]=count
    if eachWord in d:
        d[eachWord]=count
您不断将每个键的值重置为最新的行号。相反,请尝试使用
collections.defaultdict
使每个值默认以列表开头,并枚举行以获得计数:

import collections

def lineIndex(fName):
    d = collections.defaultdict(list)
    with open(fName) as f:
        for idx,line in enumerate(f):
            for word in set(line.split()):
                d[word].append(idx)
    return d

这应该适合您:

from collections import defaultdict
with open('your_file.txt','r') as f:
    result = defaultdict(set)
    counter =0
    for line in f:
        for item in line.split():
            result[item].add(counter)
        counter +=1
    print {i[0]:list(i[1]) for i in result.items()}
输出:

{'no': [0, 1], 'I': [0, 1], 'gotta': [2], 'it': [2, 3], 'rain': [2, 3], 
'shame': [1], 'have': [0, 1], 'You': [2], 'pride': [0], 'Make': [3], 'make': [2]}

这应该适合您:

from collections import defaultdict
with open('your_file.txt','r') as f:
    result = defaultdict(set)
    counter =0
    for line in f:
        for item in line.split():
            result[item].add(counter)
        counter +=1
    print {i[0]:list(i[1]) for i in result.items()}
输出:

{'no': [0, 1], 'I': [0, 1], 'gotta': [2], 'it': [2, 3], 'rain': [2, 3], 
'shame': [1], 'have': [0, 1], 'You': [2], 'pride': [0], 'Make': [3], 'make': [2]}

没有任何导入模块的替代解决方案:

d = {}
with open("rain.txt") as f:
    for i,line in enumerate(f.readlines()):
        for word in line.split():
            if word in d:
                if i not in d[word]:
                    d[word].append(i)
            else:
                d[word] = [i]
print(d)                
结果如下所示:

{'no': [0, 1], 'gotta': [2], 'make': [2], 'rain': [2, 3], 'I': [0, 1], 
'You': [2], 'Make': [3], 'have': [0, 1], 'pride': [0], 'it': [2, 3], 
'shame': [1]}
未列举的备选方案:

d = {}
with open("rain.txt") as f:
    frl = f.readlines()
    for i in range(len(frl)):
        line=frl[i]
        for word in line.split():
            if word in d:
                if i not in d[word]:
                    d[word].append(i)
            else:
                d[word] = [i]
print(d)                

没有任何导入模块的替代解决方案:

d = {}
with open("rain.txt") as f:
    for i,line in enumerate(f.readlines()):
        for word in line.split():
            if word in d:
                if i not in d[word]:
                    d[word].append(i)
            else:
                d[word] = [i]
print(d)                
结果如下所示:

{'no': [0, 1], 'gotta': [2], 'make': [2], 'rain': [2, 3], 'I': [0, 1], 
'You': [2], 'Make': [3], 'have': [0, 1], 'pride': [0], 'it': [2, 3], 
'shame': [1]}
未列举的备选方案:

d = {}
with open("rain.txt") as f:
    frl = f.readlines()
    for i in range(len(frl)):
        line=frl[i]
        for word in line.split():
            if word in d:
                if i not in d[word]:
                    d[word].append(i)
            else:
                d[word] = [i]
print(d)                


我希望我知道,现在还不太清楚您是如何获得这些输出的。你能解释一下它背后的逻辑吗?希望我能解释清楚,你是如何得到这些输出的。你能解释一下背后的逻辑吗?@TigerhawkT3,我也包括了输出。我不清楚您的反对意见。抱歉,没有注意到您已将其初始化为一个集合。当然,集合上的顺序是任意的,因此您可能无法获得每个值的排序列表。没问题,没关系。坦率地说,现在你的方法似乎更好+1是的,我可以用排序的
来修复它,但是这对于这个任务来说太多了,让我们保持原样,看看OP是怎么想的。你的
集合(line.split())
显然是个更好的主意。@TigerhawkT3,我也包括了输出。我不清楚您的反对意见。抱歉,没有注意到您已将其初始化为一个集合。当然,集合上的顺序是任意的,因此您可能无法获得每个值的排序列表。没问题,没关系。坦率地说,现在你的方法似乎更好+1是的,我可以用排序的
来修复它,但是这对于这个任务来说太多了,让我们保持原样,看看OP是怎么想的。您的
集合(line.split())
显然是一个更好的主意。可以用简单的for循环来完成吗?我们还没有学习过idx,枚举(f)中的行。它可以用简单的for循环完成吗?我们还没有学习过idx,枚举(f)中的行。它可以用简单的for循环完成吗?对于idx,我们尚未学习枚举(f)中的行。对于rangeenumerate中的i(f.readlines()):对于行中的字。拆分():如果d中的字:如果我不在d[word]:d[word]。追加(i)否则:d[word]=[i]打印(d)最后一条注释被意外发送。frl=f.readlines()表示长度中的i(范围(frl)):line=frl[i]表示行中的字。split():如果d中的字:如果我不在d[word]:d[word]。追加(i)否则:d[word]=[i]可以用简单的for循环完成吗?对于idx,我们尚未学习枚举(f)中的行。对于rangeenumerate中的i(f.readlines()):对于行中的字。拆分():如果d中的字:如果我不在d[word]:d[word]。追加(i)否则:d[word]=[i]打印(d)最后一条注释被意外发送。frl=f.readlines()表示长度中的i(范围(frl)):line=frl[i]表示行中的字。split():如果d中的字:如果我不在d[word]:d[word]。追加(i)其他:d[word]=[i]