dictionary update sequence element error

你。 提交于 2019-12-11 04:27:24

问题


I have several dictionary files, I want this code to open each file and add it to a set, for later comparison and matching. Basically I have a different list of all permutations of all possible characters and I need to know if permutation is in dictionary. But when I try to make a set with all dictionary lines I get this error:

choices = ['s','m','o','k','e','j','a','c','k']
def parsed(choices): 
    mySet = {}

    for item in choices: 
        filename = self.location + "/dicts/%s.txt" % (item)
            mySet.update(open(filename).read().splitlines())

    return mySet  

I get this error

error: dictionary update sequence element #0 has length 4; 2 is required

Furthermore, I'd like to ask if there's a possible comparison method between two sets of data (9 character permutations, and 9 dictionary files list) that runs in less than 1 minute.

I understand that there are already questions regarding this error, but frankly I'm a beginner and I don't understand how those relate to my code, or how to fix it.


回答1:


If you write:

mySet = {}

mySet is not a set, but a dictionary (yeah that is confusing). For instance:

>>> type({})
<class 'dict'>

In order to construct an empty set, you should use:

mySet = set()

A set indeed has a function update that takes as input an iterable of elements that are all added to the set. A dictionary on the other hand requires an iterable of tuples (or a dictionary, etc.)



来源:https://stackoverflow.com/questions/42203482/dictionary-update-sequence-element-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!