簡體   English   中英

python無法正確輸出字典

[英]python can not get the output right for dictionary

該文件包含以下字符串:

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

這應該為您工作:

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)                

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM