Questions tagged [dictionary-comprehension]
A syntactic construct in Python which provides a concise way to create dictionaries.
dictionary-comprehension
880
questions
0
votes
4
answers
90
views
Finding keywords in lists inside a dictionary
I have a dictionary that will have multiple dictionary / lists inside of it and I want to know how to go through each and search for keywords and add them total.
Example
dict1 = { "subDict1":...
0
votes
1
answer
41
views
Compressing a list of low bit integers (2, 3, 4 bits)
I have a relatively long list of low bit integers by size 16*10^6.
The sparsity level of this list is about 40%, so applying any sparse matrix does not work.
The first step I tried Run-length encoding,...
-1
votes
1
answer
27
views
Verify if items of list are present in a another list of dictionaries [duplicate]
I have a list that looks like this:
fruits = [apple,grapes,banana]
I have another list called:
attributes :
[{'name': 'apple', 'color': 'red', 'price': '2'},
{'name': 'orange', 'color': 'orange', '...
0
votes
0
answers
29
views
Dictionary Comprehension with Pandas Data Frame, how to get all occurrences of a match? [duplicate]
name,age
ben,28
laura,29
tom,28
The above is the structure of my CSV file.
my_dict = {row.age: row for (index, row) in data.iterrows()}
When I print(my_dict) I only see Tom and not Ben, why is one ...
0
votes
1
answer
20
views
Dict comprehension over multiple dicts with missing keys
I have a piece of legacy python2 code that looks like the following
# dict1 and dict2 are inputs. dict2 is where the value is a 4-tuple
return {
key: [
NamedTuple1(val1, ...
1
vote
2
answers
37
views
normalizing weights efficiently in python
I want to normalize weights within subgroups in a list/array. I have a working version now but it looks cumbersome and i feel there is a much more efficient way to do this. Btw, I have experience with ...
0
votes
0
answers
18
views
Rego object comprehension seems incomprehensible to me; lowercase the keys
In rego, I want to convert this:
d := {"a": "aye", "B": "bEe"}
to:
l := {"a": "aye", "b": "bEe"}
where the keys are ...
0
votes
2
answers
43
views
How to add an object to a list in a dictionary using class
I have two classes: Fit class and Fit_collection which is collection of instances of Fit class. I also have two lists each element of lists need to be converted to Fit object and need to be saved to ...
2
votes
1
answer
103
views
Iterate over values of nested dictionary
Nested_Dict =
{'candela_samples_generic': {'drc_dtcs': {'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all':{
'0x930001': {'identification': {'udsDtcValue': '0x9300', 'FaultType': '0x11', '...
1
vote
1
answer
66
views
Aggregating Inputs into a Consolidated Dictionary
Nested_Dict =
{'candela_samples_generic': {'drc_dtcs': {'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all':{
'0x930001': {'identification': {'udsDtcValue': '0x9300', 'FaultType': '0x11', 'description': '...
0
votes
1
answer
33
views
Can I made use of comprehensions for comparing these lists of dictionaries?
As title. I'm trying to figure out a comprehension for this. Getting stuck adding complexity to the basic code I built so far. I feel like this would actually be easier in a comprehension, but I ...
2
votes
1
answer
175
views
RuntimeError: await wasn't used with future - async dict comprehension in python3.8
I've finished this tutorial about async list comprehension. Now I want to try an async dict comprehension. This is the code to replicate:
import asyncio
async def div7_tuple(x):
return x, x/7
...
1
vote
2
answers
89
views
Dict comprehension taking much longer than expected
I'm profiling my simulation code, and I noticed a dict comprehension was taking quite a while.
The dict comprehension looks like this:
return {
n : self.method(d) for n, d in zip(ns, ds)
}
The ...
0
votes
0
answers
37
views
'ValueError: could not convert string to float: '5,742.86' [duplicate]
page=requests.get(url).text
table_attribs=["Name","mc_usd_millions"]
df=pd.DataFrame(columns=table_attribs)
data=BeautifulSoup(page,'html.parser')
tables=data.find_all('tbody')
...
1
vote
1
answer
160
views
Word file to Python dictionary
I'm trying to turn a *.docx file with questions into a python dictionary.
The questions have this format:
Question
a. first answer
b. second answer
c. third answer
d. fourth answer
e. fifth answer
...