How could I change this to a for loop? [closed] - python

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.

Related

How to count frequency of multiple items in a list and print relative frequencies [closed]

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
Given two lists, I need to count the frequency of the items in one list as they are found in the other list; and place the relative frequencies of each item inside frequencyList (where the
frequency of searchFor[0] is stored in frequencyList[0])
I am unable to import anything
textList=['a','b','a','c',...]
searchFor=['a','b']
frequencyList=[2,1]
Try:
[textList.count(i) for i in searchFor]
Or?
list(map(textList.count, searchFor))
The other answer is quite compact and very pythonic but this is an alternate solution that is slightly more efficient as it only requires one pass over the input list.
textList=['a','b','a','c']
output_dict = {}
for i in textList:
try:
output_dict[i] = d[i] + 1
except:
output_dict[i] = 1
print(output_dict['a'])

Convert list of list into dictionary [closed]

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)

Converting list of array to a list? [closed]

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

max() function on list with an integrer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list composed of integers, and I would like to do this :
freeSushi = max(sushiPrices <= sushiPrice)
sushiPrice being an integer and sushiPrices being a list.
Any idea of how I can do this ?
You can use filter before applying max
max(filter(lambda price: price <= sushiPrice, sushiPrices)
This would work, if "something" was a list of prices less than or equal to sushiPrice:
freeSushi = max(something)
So how can we create a list of prices less than sushiPrice? Comprehend?
Use a for loop like so:
for i in sushiPrices:
if i > sushiPrice:
del sushiPrices[sushiPrices.index(i)]
freeSushi = max(sushiPrices)

How to print certain elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How do I print certain elements of a list for example:
list1=["0","0","0","0","0","0","Element1","0","0","0","0"]
Is there any simple way to print only Element1 that specifies that you should not print out anything that is equal to 0.
Use a list comprehension or (as in this example) a generator expression to filter out the "0" items, and loop through the filtered list:
for item in (x for x in list1 if x != "0"):
print(item)
This prints all items that are not "0".

Categories