list-comprehension

How can I use list comprehension to get some data from another list?

有些话、适合烂在心里 提交于 2020-01-30 10:49:43
问题 Hello I have the following list : a = [{'Hello':5, 'id':[{'cat':'billy', 'dog': 'Paul'}, {'cat':'bill', 'dog': 'Pau'}]}, {'Hello':1, 'id':[{'cat':'Harry', 'dog': 'Peter'}, {'cat':'Hary', 'dog': 'Pete'}]}] and I would like to build the following list (using list comprehensions): b = ['billy', 'bill', 'Hary', 'Harry'] I tried these without success: [x for y in a for b in y['id'] for x in b] [x for y in a for b in y['id'] for x in b['cat']] 回答1: If you want to use a double loop: [x['cat'] for y

Comprehension to find the min in a dict

僤鯓⒐⒋嵵緔 提交于 2020-01-30 09:19:50
问题 I am curious about a thing: I have a dict, for example with a car as key and a value regarding its speed. Now, I want to find the key with the lowest value. car_dict = {'mercedes': 200, 'fiat': 100, 'porsche': 300, 'rocketcar': 600} I know this code works with O(1) car_value = list(car_dict.values()) car_key = list(car_dict.keys()) min_stats = min(car_value) print(car_key[car_value.index(min_stats)]) and this one too with O(n) keys = [] values = [] for name, value in car_dict.items(): keys

Comprehension to find the min in a dict

让人想犯罪 __ 提交于 2020-01-30 09:19:14
问题 I am curious about a thing: I have a dict, for example with a car as key and a value regarding its speed. Now, I want to find the key with the lowest value. car_dict = {'mercedes': 200, 'fiat': 100, 'porsche': 300, 'rocketcar': 600} I know this code works with O(1) car_value = list(car_dict.values()) car_key = list(car_dict.keys()) min_stats = min(car_value) print(car_key[car_value.index(min_stats)]) and this one too with O(n) keys = [] values = [] for name, value in car_dict.items(): keys

Read an integer list from single line input along with a range using list comprehension in Python 3

时光毁灭记忆、已成空白 提交于 2020-01-30 09:17:09
问题 How to read an integer list from single line input along with a range in Python 3? Requirement: reading integer values for a given list separated by a space from single line input but with a range of given size. example: Range = 4 Then list size = 4 Then read the input list from a single line of size 4 I tried below list comprehension statement, but it is reading a list from 4 lines [i.e creating 4 lists with each list representing values from a given line] instead of reading only 1 list with

Use `for` in `print()` will give a generator on Python 3.x? [duplicate]

纵然是瞬间 提交于 2020-01-30 08:29:06
问题 This question already has answers here : Need to understand Python generator object (4 answers) Closed 5 months ago . Why and how does it works? For example I'm writing a list comprehension like this: >>> a = (10, 30, 20) >>> print([q for q in a]) [10, 30, 20] At now, if I remove the [] , this will also work, but: >>> a = (10, 30, 20) >>> print(q for q in a) <generator object <genexpr> at 0x7fe527d1dca8> Does Python make a generator here? And if I do this without print() : >>> a = (10, 30, 20

Turning a list of dictionaries into a list of lists

a 夏天 提交于 2020-01-30 06:30:08
问题 I know this is possible with list comprehension but I can't seem to figure it out. Currently I have a list of dictionaries like so: [ {'field1': 'a', 'field2': 'b'}, {'field1': 'c', 'field2': 'd'}, {'field1': 'e', 'field2': 'f'} ] I'm trying to turn this into: list = [ ['b', 'a'], ['d', 'c'], ['f', 'e'], ] 回答1: You can try: [[x['field2'], x['field1']] for x in l] where l is your input list. The result for your data would be: [['b', 'a'], ['d', 'c'], ['f', 'e']] This way you ensure that the

Filter list of strings using list comprehension

若如初见. 提交于 2020-01-30 05:18:13
问题 >>> li = ["a b self", "mpilgrim", "foo c", "b", "c", "b", "d", "d"] >>> condition = ["b", "c", "d"] >>> [elem for elem in li if elem in condition] ['b', 'c', 'b', 'd', 'd'] But is there a way to return ['a b self','foo c','b', 'c', 'b', 'd', 'd'] Since b and c are included in 'a b self' and 'foo c' , I want the code to return the two as well. 回答1: Assuming the code needs to retrieve all the strings that contain any of the conditions strings: [elem for elem in li if any(c in elem for c in

proper use of list comprehensions - python

百般思念 提交于 2020-01-27 23:35:04
问题 Normally, list comprehensions are used to derive a new list from an existing list. Eg: >>> a = [1, 2, 3, 4, 5] >>> [i for i in a if i > 2] [3, 4, 5] Should we use them to perform other procedures? Eg: >>> a = [1, 2, 3, 4, 5] >>> b = [] >>> [b.append(i) for i in a] [None, None, None, None, None] >>> print b [1, 2, 3, 4, 5] or should I avoid the above and use the following instead?: for i in a: b.append(i) 回答1: You should indeed avoid using list comprehensions (along with dictionary

In Python, is it better to use list comprehensions or for-each loops?

孤街浪徒 提交于 2020-01-26 11:47:54
问题 Which of the following is better to use and why? Method 1: for k, v in os.environ.items(): print "%s=%s" % (k, v) Method 2: print "\n".join(["%s=%s" % (k, v) for k,v in os.environ.items()]) I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions are still somewhat foreign to me. Is the second way considered more Pythonic? I'm assuming there's no performance difference, but I may be wrong. What would be the advantages

list comprehension returning “generator object…”

人走茶凉 提交于 2020-01-24 09:49:16
问题 I'm trying to succinctly create a list from a dictionary. The following code works: def main(): newsapi = NewsApiClient(api_key=API_KEY) top_headlines = newsapi.get_everything(q="Merkel",language="en") news = json.dumps(top_headlines) news = json.loads(news) articles = [] for i in news['articles']: articles.append(i['title']) print(articles) output: ['Merkel “Helix Suppressor” Rifle and Merkel Suppressors', 'Angela Merkel', 'Merkel says Europe should do more to stop Syria war - Reuters',