Python 3.x 如何按顺序逐个遍历dic中具有相同值的所有键

Python 3.x 如何按顺序逐个遍历dic中具有相同值的所有键,python-3.x,dictionary,Python 3.x,Dictionary,我正在处理一个包含太多单词的文本文件,我想得到所有单词的长度。例如,首先我想得到所有长度为2,3,然后4到15的单词 单词=这个,长度=4 仇恨:4 爱情:4 那是:4 中国:5 太好了:5 以此类推,最多15个 我试着用下面的代码,但我不能一个接一个地遍历所有的键。通过这段代码,我可以得到长度为5的单词,但我希望这个循环以顺序从2开始,一直到15 text = open(r"C:\Users\israr\Desktop\counter\Bigdata.txt") d = dict()

我正在处理一个包含太多单词的文本文件,我想得到所有单词的长度。例如,首先我想得到所有长度为2,3,然后4到15的单词 单词=这个,长度=4 仇恨:4 爱情:4 那是:4 中国:5 太好了:5 以此类推,最多15个

我试着用下面的代码,但我不能一个接一个地遍历所有的键。通过这段代码,我可以得到长度为5的单词,但我希望这个循环以顺序从2开始,一直到15

text = open(r"C:\Users\israr\Desktop\counter\Bigdata.txt") 

d = dict() 

 for line in text:  
    line = line.strip() 

    line = line.lower() 

    words = line.split(" ") 
    for word in words:
        if word not in d: 
            d[word] = len(word) 

def getKeysByValue(d, valueToFind):
    listOfKeys = list()
    listOfItems = d.items()
    for item  in listOfItems:
        if item[1] == valueToFind:
            listOfKeys.append(item[0])
    return  listOfKeys
listOfKeys = getKeysByValue(d, 5)

print("Keys with value equal to 5")
#Iterate over the list of keys
for key  in listOfKeys:
     print(key)

您的第一部分代码是正确的,dictionaryd将为您提供所有具有各自长度的唯一单词。 现在,您需要获取所有单词及其长度,如下所示:

{'this':4, 'that':4, 'water':5, 'china':5, 'great':5.......till length 15}
要获得这样的字典,您可以按其值对字典进行排序,如下所示

import operator
sorted_d = sorted(d.items(), key=operator.itemgetter(1))
已排序的\u d将采用以下格式:

{'this':4, 'that':4, 'water':5, 'china':5, 'great':5,......., 'abcdefghijklmno':15,...}
我所做的是:

  • 更改了词典的结构:
    在您的字典版本中,“单词”必须是与其长度相等的键。
    如下所示:
    {“恨”:4,“爱”:4}

    新版本:
    {4:[“恨”、“爱”]、5:[“伟大”、“中国”]}

    现在键是整数,值是单词列表。例如,如果key为4,则该值将是文件中长度为4的所有单词的列表
  • 之后,代码将根据从文件读取的数据填充字典。如果字典中不存在该键,则会创建该键,否则会针对该键将单词添加到列表中
  • 键被排序并打印其值。也就是说,该长度的所有单词都是按顺序打印的
  • 您忘记关闭代码中的文件。当程序完成执行时,释放程序正在使用的任何资源是一种良好的做法。(以避免资源或内存泄漏和其他此类错误)。大多数情况下,这可以通过关闭该资源来完成。例如,关闭该文件将释放该文件,因此其他程序现在可以使用该文件
  • #2020年4月24日 #上午03:11(格林尼治标准时间+05) #塔尔哈·阿斯加尔 #打开要从中读取数据的文件 myFile=open(r“books.txt”) #创建一个空字典,我们将在其中存储字数 #字典中的数据格式为: #{1:[长度为1的文件中的字]、2:[长度为2的文件中的字]、…..等等} d=dict() #迭代文件的所有行 对于myFile中的行: #从当前行中获取单词 words=line.lower().strip().split(“”) #迭代当前行中的每个单词 用文字表示: #得到这个单词的长度 长度=长度(字) #字典里没有这么长的词 #根据此长度创建一个列表 #长度是键,值是具有此长度的单词列表 如果长度不以d.键()为单位: d[长度]=[字] #如果已有此长度的单词,请将当前单词附加到该列表中 其他: d[长度]。追加(word) 对于已排序(d.keys())中的键: 打印(键,end=“:”) 打印(d[键]) myFile.close()

    # 24-Apr-2020 # 03:11 AM (GMT +05) # TALHA ASGHAR # Open the file to read data from myFile = open(r"books.txt") # create an empty dictionary where we will store word counts # format of data in dictionary will be: # {1: [words from file of length 1], 2:[words from file of length 2], ..... so on } d = dict() # iterate over all the lines of our file for line in myFile: # get words from the current line words = line.lower().strip().split(" ") # iterate over each word form the current line for word in words: # get the length of this word length = len(word) # there is no word of this length in the dictionary # create a list against this length # length is the key, and the value is the list of words with this length if length not in d.keys(): d[length] = [word] # if there is already a word of this length append current word to that list else: d[length].append(word) for key in sorted(d.keys()): print(key, end=":") print(d[key]) myFile.close()