list-comprehension

Python. Iterate over a list of files, finding same filenames but different extensions

大兔子大兔子 提交于 2020-05-01 05:07:20
问题 So I have a list as follows: mylist = ['movie1.mp4','movie2.srt','movie1.srt','movie3.mp4','movie1.mp4'] Note: a simple list for testing, the script will deal with unknown file names and more of them. So I want to find the movie files with a paired srt file, and put those in a dictionary. Anything left (ie movie3.mp4) will be left in the list and dealt with later. I've been playing a bit with list comprehension, though it might not leave the leftover data and allow me to construct the

Python. Iterate over a list of files, finding same filenames but different extensions

♀尐吖头ヾ 提交于 2020-05-01 05:06:59
问题 So I have a list as follows: mylist = ['movie1.mp4','movie2.srt','movie1.srt','movie3.mp4','movie1.mp4'] Note: a simple list for testing, the script will deal with unknown file names and more of them. So I want to find the movie files with a paired srt file, and put those in a dictionary. Anything left (ie movie3.mp4) will be left in the list and dealt with later. I've been playing a bit with list comprehension, though it might not leave the leftover data and allow me to construct the

Python - filling a list of tuples with zeros in places of missing indexes

核能气质少年 提交于 2020-04-30 07:46:05
问题 I have a list of tuples: [(0.0, 287999.70000000007), (1.0, 161123.23000000001), (2.0, 93724.140000000014), (3.0, 60347.309999999983), (4.0, 55687.239999999998), (5.0, 29501.349999999999), (6.0, 14993.920000000002), (7.0, 14941.970000000001), (8.0, 13066.229999999998), (9.0, 10101.040000000001), (10.0, 4151.6900000000005), (11.0, 2998.8899999999999), (12.0, 1548.9300000000001), (15.0, 1595.54), (16.0, 1435.98), (17.0, 1383.01)] As can be seen, there are missing indexes (13 and 14). I want to

Python Nested List Comprehension with If Else

旧巷老猫 提交于 2020-03-25 16:15:54
问题 I was trying to use a list comprehension to replace multiple possible string values in a list of values. I have a list of column names which are taken from a cursor.description ; ['UNIX_Time', 'col1_MCA', 'col2_MCA', 'col3_MCA', 'col1_MCB', 'col2_MCB', 'col3_MCB'] I then have header_replace ; {'MCB': 'SourceA', 'MCA': 'SourceB'} I would like to replace the string values for header_replace.keys() found within the column names with the values. I have had to use the following loop; headers = []

Merge python lists of different lengths

混江龙づ霸主 提交于 2020-03-15 05:54:28
问题 I am attempting to merge two python lists, where their values at a given index will form a list (element) in a new list. For example: merge_lists([1,2,3,4], [1,5]) = [[1,1], [2,5], [3], [4]] I could iterate on this function to combine ever more lists. What is the most efficient way to accomplish this? Edit (part 2) Upon testing the answer I had previously selected, I realized I had additional criteria and a more general problem. I would also like to combine lists containing lists or values.

Merge python lists of different lengths

六月ゝ 毕业季﹏ 提交于 2020-03-15 05:53:27
问题 I am attempting to merge two python lists, where their values at a given index will form a list (element) in a new list. For example: merge_lists([1,2,3,4], [1,5]) = [[1,1], [2,5], [3], [4]] I could iterate on this function to combine ever more lists. What is the most efficient way to accomplish this? Edit (part 2) Upon testing the answer I had previously selected, I realized I had additional criteria and a more general problem. I would also like to combine lists containing lists or values.

Can't use locals() in list comprehension in Python 3?

有些话、适合烂在心里 提交于 2020-03-03 17:58:54
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

Can't use locals() in list comprehension in Python 3?

眉间皱痕 提交于 2020-03-03 17:58:20
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

Can't use locals() in list comprehension in Python 3?

本小妞迷上赌 提交于 2020-03-03 17:58:10
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

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

99封情书 提交于 2020-01-30 10:51:02
问题 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