list-comprehension

Handling empty case with tuple filtering and unpacking

最后都变了- 提交于 2021-02-20 09:33:23
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

Handling empty case with tuple filtering and unpacking

大兔子大兔子 提交于 2021-02-20 09:32:28
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

Handling empty case with tuple filtering and unpacking

浪尽此生 提交于 2021-02-20 09:31:43
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

List Comprehensions in Python to compute minimum and maximum values of a list

此生再无相见时 提交于 2021-02-20 06:25:26
问题 i have the following code to compute minimum and maximum values of a list in order to save memory efficiency x_min = float('+inf') x_max = float('-inf') for p in points_in_list: x_min = min(x_min, p) x_max = max(x_max, p) where points_in_list is a (large) list of numbers. I wish to know if there is a method to compute with a List Comprehensions the min and max value and saving the memory. 回答1: I'm a big fan of generators and comprehensions, but in this case it seems they are not the right way

Python - keeping counter inside list comprehension

妖精的绣舞 提交于 2021-02-19 04:34:45
问题 Is it possible to write a list comprehension for the following loop? m = [] counter = 0 for i, x in enumerate(l): if x.field == 'something': counter += 1 m.append(counter / i) I do not know how to increment the counter inside the list comprehension. 回答1: You could use an itertools.count: import itertools as IT counter = IT.count(1) [next(counter)/i for i, x in enumerate(l) if x.field == 'something'] To avoid the possible ZeroDivisionError pointed out by tobias_k, you could make enumerate

List of dictionaries with comprehension in python

ⅰ亾dé卋堺 提交于 2021-02-18 11:22:31
问题 I have the following list of dictionaries: ld=[{'a':10,'b':20},{'p':10,'u':100}] I want to write a comprehension like this: [ (k,v) for k,v in [ d.items() for d in ld ] ] basically I want to iterate over dictionaries in the list and get the keys and values of each dict and do sth. Example: One example output of this would be for example another list of dictionaries without some keys: ld=[{'a':10,'b':20},{'p':10,'u':100}] new_ld=[{'a':10},{'p':10}] However, the above comprehension is not

Combine Python List and Dict comprehension with counter

僤鯓⒐⒋嵵緔 提交于 2021-02-17 02:22:30
问题 I want to transfer a list of tuples: [(1, 3, 5), (2, 4, 6), (7, 8, 9)] to a list of dict (in order to create a pandas dataframe) which looks like: [{'index':1, 'match':1},{'index':1, 'match':3},{'index':1, 'match':5}, {'index':2, 'match':2}, {'index':2, 'match':4},{'index':2, 'match':6}, {'index':3, 'match':7},{'index':3, 'match':8},{'index':3, 'match':9}] For performance reasons I wanted to use a list and dict comprehension: [{'index':ind, 'match': } for ind, s in enumerate(test_set, 1)] How

Async List Comprehensions in Python

谁都会走 提交于 2021-02-11 12:31:46
问题 Is it possible to do a list comprehension in Python asynchronously? I am trying to make many calls to a web api, where the bulk of the wait time is waiting for data to come back, which I would like to do asynchronously. Then after the data is fetched, I want to return the data and store it in a variable. How would I go about doing this? Below is the non asynchronous code: api_data = [{data_name: get_data(data_name)} for data_name in data_name_list] Below is my attempted asynchronous solution:

Unable to remove duplicate dicts in list using list comprehension or frozenset

心不动则不痛 提交于 2021-02-10 18:18:37
问题 I would like to remove duplicate dicts in list. Specifically, if two dict having the same content under the key paper_title, maintain one and remove the other duplicate. For example, given the list below test_list = [{"paper_title": 'This is duplicate', 'Paper_year': 2}, \ {"paper_title": 'This is duplicate', 'Paper_year': 3}, \ {"paper_title": 'Unique One', 'Paper_year': 3}, \ {"paper_title": 'Unique two', 'Paper_year': 3}] It should return return_value = [{"paper_title": 'This is duplicate'

List comprehension with tuple assignment

て烟熏妆下的殇ゞ 提交于 2021-02-10 13:42:30
问题 I want to ask if something like this is possible in python: a,b = [i,i+1 for i in range(5)] I know this isn't possible because I have got an error, but I think you understand what I am trying to achieve. Let me clear it up, I can do : a,b = 3+2,3 Edit ---> Or even better: a,b = [0,1,2,3,4],[1,2,3,4,5] I wan't a similar thing in my first code example. I am trying to assign variables 'a' and 'b' as list, with list comprehension, but using tuple as assignment, the point is I don't want to use