Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a list of list as follows
my_list = [['val1','12'],['val2', 'disabled'],['apple', 10],['banana', 20]]
i would like to convert my_list into dictionary such that my_dict looks like
my_dict = {'val1':'12','val2':'disabled','apple':10, 'banana:20}
Just issue dict(my_list). The dict constructor accepts any iterable of two-element iterables.
You could write your own code like:
my_dict = {}
for i in my_list:
my_dict[i[0]] = i[1]
print(my_dict)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list of dictionaries and want each dictionary to be sorted by value
list=[{ 1:0.2, 3:0.1, 5:0.8},{ 6:0.8, 9:0.6, 10:0.7}]
when sorted by value it should become:
[{ 3:0.1, 1:0.2, 5:0.8},{ 9:0.6, 10:0.7, 6:0.8}]
Starting from python 3.7 dictionaries are insertion ordered, so you could do it in the following way:
lst = [dict(sorted(d.items(), key=lambda x: x[1])) for d in lst]
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I don't understand inner workings of dictionary comprehension loop. How could I change this to a for loop?
s_f={node:-1 for node in adj_list.keys()}
It's equivalent to this:
s_f = {}
for node in ajd_list.keys():
s_f[node] = -1
A dictionary comprehension is equivalent to assigning to the key of the resulting dictionary each time through the loop, just as a list comprehension is equivalent to calling append() on the resulting list in the loop.
s_f={node:-1 for node in adj_list.keys()}
is the same as
sf = {}
for node in adj_list.keys():
sf[node] = -1
keep in mind that comprehension is usually faster than a for loop, especially with large data.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to remove the amp; from all the data inside the list
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
desired output without amp;
['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11',...]
Using re.sub():
import re
data = ['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
data = [re.sub(r'amp;', '', item) for item in data]
A simple list comprehension is best using replace
my_list=['?daypartId=1&catId=12', '?daypartId=1&catId=1', '?daypartId=1&catId=2', '?daypartId=1&catId=11', '?daypartId=1&catId=10', '?daypartId=1&catId=6', '?daypartId=1&catId=4', '?daypartId=1&catId=14', '?daypartId=1&catId=5', '?daypartId=1&catId=3', '?daypartId=1&catId=8']
[i.replace('&','') for i in my_list]
using regex
import re
[re.sub('&','',i) for i in my_list]
Output
['?daypartId=1;catId=12',
'?daypartId=1;catId=1',
'?daypartId=1;catId=2',
'?daypartId=1;catId=11',
'?daypartId=1;catId=10',
'?daypartId=1;catId=6',
'?daypartId=1;catId=4',
'?daypartId=1;catId=14',
'?daypartId=1;catId=5',
'?daypartId=1;catId=3',
'?daypartId=1;catId=8']
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a list of arrays something like this:
list:
[[['23456', '23456']], [['459687', '201667'],['769687', '203416']]
How can I remove the the nested [] to have a list of list something like this:
list:
[['23456', '23456'], ['459687', '201667'],['769687', '203416']]
Any idea?
new_list = []
for sub_list in nested_list:
if type(sub_list[0]) == list:
for potential_list in sub_list:
if type(potential_list) == list:
new_list.append(potential_list)
elif type(sub_list[0]) == str:
new_list.append(sub_list)
else:
print(type(sub_list)) # if you get here, you have even more weird nesting than in your example
This will handle your example but won't handle nesting deeper than the example. If you need deeper nesting create a function similar to the following but use recursion
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Say I have
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
and I want to cast all of those numbers into just ints to have
A = [[1,2,1],[2,1,3]]
How do we do that in python?
Try list comprehension*2:
print([[int(x) for x in i] for i in A])
Or list comprehension + map:
print([list(map(int,i)) for i in A])
Or map+map:
print(list(map(lambda x: list(map(int,x)),A)))
Simple ways all return:
[[1,2,1],[2,1,3]]
Here's an approach using a list comprehension and map:
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
print([list(map(int, i)) for i in A])