list-comprehension

How to check if object is not None within a list comprehension?

不羁的心 提交于 2020-08-24 06:26:43
问题 I am somehow familiar with list comprehensions in Python. But in situations when I need to check if the list is not None, the list comprehension would fail. e.g. tags = v.tags if tags: for t in tags: if t['Key'] == 'Name': # Do something Now, if tags is None, then the following list comprehension fails. It works fine if tags is empty/[]. I would like a list comprehension that checks against None. [k for k,v in tags if tags]: 回答1: How about this: [k for k in (tags or [])] Let's see what

Unusual list comprehension behaviour

旧街凉风 提交于 2020-08-10 22:31:34
问题 I'm trying to port some code from Python to R and I've come across a list comprehension I cannot fully understand. Here is a toy example analogous to the code import numpy as np theta = np.random.rand(5, 2, 2, 3) thetai = theta[0] logp = [theta[np.newaxis, ...] for theta in thetai] If I run and print the results I get: print(logp) [array([[[0.779, 0.461, 0.766], [0.245, 0.189, 0.045]]]), array([[[0.229, 0.288, 0.173], [0.011, 0.541, 0.528]]])] Ok output is a list of two arrays. What I cannot

Unusual list comprehension behaviour

Deadly 提交于 2020-08-10 22:29:14
问题 I'm trying to port some code from Python to R and I've come across a list comprehension I cannot fully understand. Here is a toy example analogous to the code import numpy as np theta = np.random.rand(5, 2, 2, 3) thetai = theta[0] logp = [theta[np.newaxis, ...] for theta in thetai] If I run and print the results I get: print(logp) [array([[[0.779, 0.461, 0.766], [0.245, 0.189, 0.045]]]), array([[[0.229, 0.288, 0.173], [0.011, 0.541, 0.528]]])] Ok output is a list of two arrays. What I cannot

Unusual list comprehension behaviour

亡梦爱人 提交于 2020-08-10 22:28:24
问题 I'm trying to port some code from Python to R and I've come across a list comprehension I cannot fully understand. Here is a toy example analogous to the code import numpy as np theta = np.random.rand(5, 2, 2, 3) thetai = theta[0] logp = [theta[np.newaxis, ...] for theta in thetai] If I run and print the results I get: print(logp) [array([[[0.779, 0.461, 0.766], [0.245, 0.189, 0.045]]]), array([[[0.229, 0.288, 0.173], [0.011, 0.541, 0.528]]])] Ok output is a list of two arrays. What I cannot

Getting Error: 'not enough values to unpack' when using list comprehension together with conditional statement in Python

谁说胖子不能爱 提交于 2020-08-05 06:17:27
问题 The objective is to create a list comprehension that outputs two values. The for loops look like below paper_href_scopus = [] paper_title = [] for litag in all_td.find_all('a', {'class': 'ddmDocTitle'}): paper_href_scopus.append(litag['href']) paper_title.append(litag.text) As suggested by OP, this can be achieved by paper_href_scopus, paper_title = zip(*[(litag['href'], litag.text) for litag in all_td.find_all('a', {'class': 'ddmDocTitle'})]) However, there is an instances where the all_td

Dictionary comprehension with if statements using list comprehension

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-21 07:29:19
问题 I am trying to filter a large dictionary based on values from another dictionary. I want to store the keys to filter on in a list. So far I have: feature_list = ['a', 'b', 'c'] match_dict = {'a': 1, 'b': 2, 'c': 3} all_dict = {'id1': {'a': 1, 'b': 2, 'c': 3}, 'id2': {'a': 1, 'b': 4, 'c': 3}, 'id3': {'a': 2, 'b': 5, 'c': 3}} filtered_dict = {k: v for k, v in all_dict.items() for feature in feature_list if v[feature] == match_dict[feature]} This returns all the ids because I think the if

Dictionary comprehension with if statements using list comprehension

三世轮回 提交于 2020-07-21 07:26:21
问题 I am trying to filter a large dictionary based on values from another dictionary. I want to store the keys to filter on in a list. So far I have: feature_list = ['a', 'b', 'c'] match_dict = {'a': 1, 'b': 2, 'c': 3} all_dict = {'id1': {'a': 1, 'b': 2, 'c': 3}, 'id2': {'a': 1, 'b': 4, 'c': 3}, 'id3': {'a': 2, 'b': 5, 'c': 3}} filtered_dict = {k: v for k, v in all_dict.items() for feature in feature_list if v[feature] == match_dict[feature]} This returns all the ids because I think the if

Use list comprehension to create a list of tuples for two different conditionals

こ雲淡風輕ζ 提交于 2020-07-10 03:12:47
问题 Is there a way to use list comprehension to create a list of tuples with two different conditions. I am interacting through a Pandas DF and I want to return an entire row in tuple if it matches either condition. The first is if the DF has nan values in any column. The other is if a column in the DF called ODFS_FILE_CREATE_DATETIME doesn't match the regex pattern for the date column. The date column is supposed to have an output that looks like this: 2005242132 . 10 number digits. So if the df