defaultdict

Add values for keys that contains similar string in python

Add values for keys that contains similar string in python Question: I have dict as values={‘P-ETH-900′:Decimal(122),’P-ETH-700′:Decimal(455),’P-BTC-900′:Decimal(12),’P-BTC-700’:Decimal(55)} I want to add the decimals that has condition like eth=Decimal(0) for i,j in values.items(): if ‘ETH’ in i: eth=eth+j So basically, for each similar items , need to sum up the decimal values. output: {‘ETH’:’577′,’BTC’:’67’} Asked By: Madan …

Total answers: 1

Managing contents of defaultdict with concurrency in python

Managing contents of defaultdict with concurrency in python Question: Some questions have looked at non-nested defaultdict behavior when multiprocessing: Using defaultdict with multiprocessing? Python defaultdict behavior possible with multiprocessing? and it seems that managing something nested like defaultdict(list) isn’t an entirely simple process, let alone something more complex like defaultdict(lambda: defaultdict(list)) import concurrent.futures from collections …

Total answers: 1

Infinite recursion error in custom class that combines dict, defaultdict, and SimpleNamespace functionality

Infinite recursion error in custom class that combines dict, defaultdict, and SimpleNamespace functionality Question: I am writing a class in python that combines the functionality of dict, defaultdict, and SimpleNamespace So far I have the following code: import warnings class fluiddict: """! A class that emulates a dictionary, while also being able to support attribute …

Total answers: 2

How can I cumulatively sum defaultdict with same parameter? (Python)

How can I cumulatively sum defaultdict with same parameter? (Python) Question: I recently started using defaultdict in python. When ım using defaultdict ı struggled to cumulative sum with same parameter. In my case ı have defaultdict like that: defaultdict(<class ‘list’>, {‘11106000022’: [(216.0, datetime.datetime(2022, 10, 5, 0, 0)), (216.0, datetime.datetime(2022, 10, 5, 0, 0)), (370.0, datetime.datetime(2022, …

Total answers: 1

Type hints for nested defaultdict

Type hints for nested defaultdict Question: What is the right way to write type hints for defaultdict(lambda: defaultdict(set))? I use Python 3.10.5 and mypy 0.971, and find mypy returns an error because var = defaultdict(lambda: defaultdict(set)) doesn’t have a type hint. Premises All keys of the first defaultdict and the second defaultdict are str. Values …

Total answers: 1

add multiple values to one key, but defaultdict only allows 2

add multiple values to one key, but defaultdict only allows 2 Question: In the CSV I’m reading from, there are multiple rows for each ID: ID,timestamp,name,text 444,2022-03-01T11:05:00.000Z,Amrita Patel,Hello 444,2022-03-01T11:06:00.000Z,Amrita Patel,Nice to meet you 555,2022-03-01T12:05:00.000Z,Zach Do,Good afternoon 555,2022-03-01T11:06:00.000Z,Zach Do,I like oranges 555,2022-03-01T11:07:00.000Z,Zach Do,definitely I need to extract each such that I will have one file per …

Total answers: 1

How can I generate a dictionary of lists in python?

How can I generate a dictionary of lists in python? Question: how can i create something like this in the kitchen : {‘2010-01-05’: [{‘activity’: ‘…’}, {‘activity’:’…’}, {‘activity’:’…’}], ‘2010-01-06’:[{‘activity’:’…’}, {‘activity’:’…’}] } if my list looks like this? my_list= [ [‘2010-01-05 12:32:05’, ‘in the kitchen’, ‘ON’], [‘2010-01-05 12:32:07’, ‘in the living room’, ‘ON’], [‘2010-01-05 12:32:08’, ‘in the …

Total answers: 1

Remove duplicate 2D dict values

Remove duplicate 2D dict values Question: I have the following list of dicts: defaultdict(<class ‘list’>, {‘192.168.20.10/32’: [{1: ‘aaaaa11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}], ‘192.168.20.20/32’: [{1: ‘aaaaa11111n’}, {2: ‘aaaaa11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}, {2: ‘bbbbb11111n’}]}) I would like to iterate over this list object and remove the duplicate list items. The …

Total answers: 2

Regrouping a list of dictionary

Regrouping a list of dictionary Question: I have a list of dictionary from a database query. I want to group the data by "id" and also add some new key-value pairs based on some existing data in the dictionary. Here is the data: data = [{‘id’: 1, ‘name’: ‘The Musical Hop’, ‘city’: ‘San Francisco’, ‘state’: …

Total answers: 1

defaultdict of defaultdict of int

defaultdict of defaultdict of int Question: How do I do defaultdict(defaultdict(int))? I tried the nested defaultdict method: def ddict(): return defaultdict(ddict) I am looking to do the following: m[‘a’][‘b’] += 1 Asked By: user3222184 || Source Answers: Try this: from collections import defaultdict m = defaultdict(lambda: defaultdict(int)) m[‘a’][‘b’] += 1 Note if you want more …

Total answers: 2