简体   繁体   English

Python-具有相同键的集合字典

[英]Python - Dictionary of sets with the same key

I'm trying to create a function that will read a text file that has one word on each line, like 我正在尝试创建一个函数来读取文本文件,该文本文件的每一行都包含一个单词,例如

afd
asmv 影视
adsasd adsasd

It will take words of the user given length and will construct a python dictionary where the key is a string of the word where the letters are sorted. 它将使用给定长度的用户单词,并将构建一个python字典,其中的键是单词的字符串,对字母进行排序。 The values will be a set of all words that have the same key. 这些值将是一组具有相同键的所有单词。 So far I have: 到目前为止,我有:

def setdict():
wordfile = argv[1]
open(wordfile, "r")
setdict = {}
for line in wordfile:
    words = line.split()
    for word in words:
        word = word.rstrip("\n")
        if word == wordlength:
            key = str(sorted(word))

I'm a little lost on how to create the sets with words that have the same key and put them in the dictionary. 我对如何使用具有相同关键字的单词创建集合并将它们放入字典中有些迷惑。 Any help would be appreciated. 任何帮助,将不胜感激。

collections.defaultdict is useful here: collections.defaultdict在这里很有用:

from collections import defaultdict
from pprint import pprint


words = defaultdict(set)

with open('input.txt') as input_file:
    for line in input_file:
        for word in line.split():
            sorted_list = sorted(word)
            sorted_str = ''.join(sorted_list)
            words[sorted_str].add(word)

pprint(words)

Of course, anything you can do with defaultdict , you can also do with dict.setdefault() : 当然,您可以使用defaultdict任何操作,也可以使用dict.setdefault()

words = dict()
with open('input.txt') as input_file:
    for line in input_file:
        for word in line.split():
            sorted_list = sorted(word)
            sorted_str = ''.join(sorted_list)
            words.setdefault(sorted_str, set()).add(word)

start with something simple 从简单的事情开始

words = ["hello","python","world"]
my_dict = {}
for word in words:
    try:
       my_dict[sorted(word)].append(word)
    except KeyError:
       my_dict[sorted(word)] = [word]

now instead of using predefined words read them from a file 现在不再使用预定义的单词,而是从文件中读取它们

  words = map(str.split,open("some_word_file.txt"))

the key here is to access the dictionary with a for loop that makes the value set available for manipulation. 此处的关键是使用for循环访问字典,该循环使值集可用于操作。 you can solve your problem by reading the file linewise (readline) and checking the following: 您可以通过逐行读取文件(readline)并检查以下内容来解决问题:

for key, value in my_dict:
    if sorted(word) == key:
        value.append(word)
    else:
        my_dict[sorted(word)] = value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM