defaultdict

How can I convert defaultdict(Set) to defaultdict(list)?

馋奶兔 提交于 2021-02-07 18:49:01
问题 I have a defaultdict(Set) : from sets import Set from collections import defaultdict values = defaultdict(Set) I want the Set functionality when building it up in order to remove duplicates. Next step I want to store this as json. Since json doesn't support this datastructure I would like to convert the datastructure into a defaultdict(list) but when I try: defaultdict(list)(values) I get: TypeError: 'collections.defaultdict' object is not callable , how should I do the conversion? 回答1: You

How can I convert defaultdict(Set) to defaultdict(list)?

牧云@^-^@ 提交于 2021-02-07 18:48:16
问题 I have a defaultdict(Set) : from sets import Set from collections import defaultdict values = defaultdict(Set) I want the Set functionality when building it up in order to remove duplicates. Next step I want to store this as json. Since json doesn't support this datastructure I would like to convert the datastructure into a defaultdict(list) but when I try: defaultdict(list)(values) I get: TypeError: 'collections.defaultdict' object is not callable , how should I do the conversion? 回答1: You

How to convert a defaultdict(list) to Pandas DataFrame

ぐ巨炮叔叔 提交于 2021-01-27 12:26:58
问题 I have a defaultdict(list) object that is of this structure: {id: [list[list]]} for example, 'a1': [[0.01, 'cat']], 'a2': [[0.09, 'cat']], 'a3': [[0.5, 'dog']], ... I'd like to conver this defaultdict(list) into a Pandas DataFrame object. I tried with the following: df = pd.DataFrame(list(my_dict.items()), columns=['id', 'category']) However, I faced a problem with my 'category' column. This is a column of list of list. I'm trying to split out the 2 values in the 'category' into 2 separate

mixing defaultdict (dict and int)

半腔热情 提交于 2021-01-27 07:43:18
问题 I have 2 example lists and what I want to achieve is to obtain a nested default dictionary with the sum of the values. The following code works nice: from collections import defaultdict l1 = [1,2,3,4] l2 = [5,6,7,8] dd = defaultdict(int) for i in l1: for ii in l2: dd[i] += ii but what I'm trying to do is to create a default key in the d dictionary: from collections import defaultdict l1 = [1,2,3,4] l2 = [5,6,7,8] dd = defaultdict(int) for i in l1: for ii in l2: dd[i]['mykey'] += ii and this

mixing defaultdict (dict and int)

情到浓时终转凉″ 提交于 2021-01-27 07:43:16
问题 I have 2 example lists and what I want to achieve is to obtain a nested default dictionary with the sum of the values. The following code works nice: from collections import defaultdict l1 = [1,2,3,4] l2 = [5,6,7,8] dd = defaultdict(int) for i in l1: for ii in l2: dd[i] += ii but what I'm trying to do is to create a default key in the d dictionary: from collections import defaultdict l1 = [1,2,3,4] l2 = [5,6,7,8] dd = defaultdict(int) for i in l1: for ii in l2: dd[i]['mykey'] += ii and this

Python `defaultdict`: Use default when setting, but not when getting

限于喜欢 提交于 2020-05-30 07:33:06
问题 Is there any way I can make a collections.defaultdict return a default constructed object when I set it... foo = defaultdict(list) foo[3].append('dsafdasf') ... but not when I try to access it? try: for word in foo[None]: print(word) except KeyError: pass 回答1: I think that what you're looking for is something like this: >>> foo = {} >>> foo.setdefault(3, []).append('dsafdasf') # Appends to default value >>> foo[None] # Raises a KeyError exception That is, instead of using collections

How to have multiple values for a key in a python dictionary?

*爱你&永不变心* 提交于 2020-05-09 06:04:53
问题 I have a case where the same key could have different strings associated with it. e.g. flow and wolf both have the same characters, if I sort them and use them as keys in a dictionary, I want to put the original strings as values. I tried in a python dict as: d = {} d["flow"] = flow d["flow"] = wolf but there is only one value associated with the key. I tried d["flow"].append("wolf") but that also doesn't work. How to get this scenario working with Python dicts? 回答1: You can't have multiple

Nested dictionary that acts as defaultdict when setting items but not when getting items

梦想的初衷 提交于 2020-05-08 04:45:46
问题 I want to implement a dict-like data structure that has the following properties: from collections import UserDict class TestDict(UserDict): pass test_dict = TestDict() # Create empty dictionaries at 'level_1' and 'level_2' and insert 'Hello' at the 'level_3' key. test_dict['level_1']['level_2']['level_3'] = 'Hello' >>> test_dict { 'level_1': { 'level_2': { 'level_3': 'Hello' } } } # However, this should not return an empty dictionary but raise a KeyError. >>> test_dict['unknown_key']