dictionary

python create dict using list of strings with length of strings as values

牧云@^-^@ 提交于 2021-02-04 19:33:14
问题 I'm sure this can be done, but I have thus far been unsuccessful: I have a list of strings. I want to create a dictionary with the length of said strings (which can be expressed as a range) as the key and the string itself as the value. example: Here's something like the list I have: ['foo','bar','help','this','guy'] I'd like to end up with a dictionary like this: {3:['foo','bar','guy], 4:['this','help']} 回答1: You can use a dictionary as a container with setdefault : lst = ['foo','bar','help'

python create dict using list of strings with length of strings as values

旧城冷巷雨未停 提交于 2021-02-04 19:32:05
问题 I'm sure this can be done, but I have thus far been unsuccessful: I have a list of strings. I want to create a dictionary with the length of said strings (which can be expressed as a range) as the key and the string itself as the value. example: Here's something like the list I have: ['foo','bar','help','this','guy'] I'd like to end up with a dictionary like this: {3:['foo','bar','guy], 4:['this','help']} 回答1: You can use a dictionary as a container with setdefault : lst = ['foo','bar','help'

Iterate over list of dict and group item by key

独自空忆成欢 提交于 2021-02-04 19:22:07
问题 I have a list of dicts: my_list = [ {'name': 'AAA', 'date': '2018-05-14', 'price': 20.0}, {'name': 'AAA', 'date': '2018-05-15', 'price': 22.0}, {'name': 'AAA', 'date': '2018-05-16', 'price': 30.0}, {'name': 'BBB', 'date': '2018-05-14', 'price': 15.0}, {'name': 'BBB', 'date': '2018-05-15', 'price': 32.0} ] My question is how can I iterate over that list to produce a list in this format? parsed_list = [ {'name': 'AAA', 'data': [['2018-05-14', 20.0], ['2018-05-15', 22.0], ['2018-05-16', 30.0]]},

Python: Read multiple lines from a file and make instances stored in an dictionary

爱⌒轻易说出口 提交于 2021-02-04 19:12:14
问题 My struggle: Reading two lines and jumping over the third. Then I want to store all the objects in a dictionary with the name as keys. **** Ingredients.txt **** Name1 ingredient1/ingredient2/ingredient3 Name2 ingredient1/ingredient2 Name3 ... class Foodset(object): def __init__(self, name): self.name = name self.ingredients = set([]) def __str__(self): return str(self.name) + ' consits of: ' + ", ".join(str(e) for e in self.ingredients) + '.' def setIngredients(self, list): for i in list:

Python dictionary “plus-equal” behavior

北战南征 提交于 2021-02-04 15:31:31
问题 I'm trying to understand the exact mechanism behind updating a python dictionary using d[key] += diff . I have some helper classes to trace magic method invocations: class sdict(dict): def __setitem__(self, *args, **kargs): print "sdict.__setitem__" return super(sdict, self).__setitem__(*args, **kargs) def __delitem__(self, *args, **kargs): print "sdict.__delitem__" return super(sdict, self).__delitem__(*args, **kargs) def __getitem__(self, *args, **kargs): print "sdict.__getitem__" return

How to wrap a python dict?

断了今生、忘了曾经 提交于 2021-02-04 15:08:39
问题 I want to implement a class that will wrap -- not subclass -- the python dict object, so that when a change is detected in a backing store I can re-create the delegated dict object. I intend to check for changes in the backing store each time the dict is accessed for a read. Supposing I was to create an object to act like this; what methods would I need to implement? 回答1: You can subclass the ABC (abstract base class) collections.Mapping (or collections.MutableMapping if you also want to

How to wrap a python dict?

依然范特西╮ 提交于 2021-02-04 15:07:52
问题 I want to implement a class that will wrap -- not subclass -- the python dict object, so that when a change is detected in a backing store I can re-create the delegated dict object. I intend to check for changes in the backing store each time the dict is accessed for a read. Supposing I was to create an object to act like this; what methods would I need to implement? 回答1: You can subclass the ABC (abstract base class) collections.Mapping (or collections.MutableMapping if you also want to

How to wrap a python dict?

时间秒杀一切 提交于 2021-02-04 15:07:49
问题 I want to implement a class that will wrap -- not subclass -- the python dict object, so that when a change is detected in a backing store I can re-create the delegated dict object. I intend to check for changes in the backing store each time the dict is accessed for a read. Supposing I was to create an object to act like this; what methods would I need to implement? 回答1: You can subclass the ABC (abstract base class) collections.Mapping (or collections.MutableMapping if you also want to

How to wrap a python dict?

蹲街弑〆低调 提交于 2021-02-04 15:07:38
问题 I want to implement a class that will wrap -- not subclass -- the python dict object, so that when a change is detected in a backing store I can re-create the delegated dict object. I intend to check for changes in the backing store each time the dict is accessed for a read. Supposing I was to create an object to act like this; what methods would I need to implement? 回答1: You can subclass the ABC (abstract base class) collections.Mapping (or collections.MutableMapping if you also want to

How to wrap a python dict?

假如想象 提交于 2021-02-04 15:07:23
问题 I want to implement a class that will wrap -- not subclass -- the python dict object, so that when a change is detected in a backing store I can re-create the delegated dict object. I intend to check for changes in the backing store each time the dict is accessed for a read. Supposing I was to create an object to act like this; what methods would I need to implement? 回答1: You can subclass the ABC (abstract base class) collections.Mapping (or collections.MutableMapping if you also want to