list-comprehension

Memory efficient way for list comprehension of pandas dataframe using multiple columns

馋奶兔 提交于 2020-06-09 04:06:10
问题 I want to run a function on rows of a pandas dataframe in list comprehension. Dataframe can have varying number of columns. How to make use these columns of dataframe? import pandas as pd df = {'chrom': ['chr1', 'chr1','chr1'], 'start': [10000, 10100, 12000], 'end':[10150,10120,12250], 'S1':[1, 1, 1],'S2':[2, 2, 2],'S3':[3, 3, 3] } df = pd.DataFrame(data=df) print(df) def func(row): print(row) [func(row) for row in zip(df['chrom'],df['start'],df['S1'],df['S2'],df['S3'])] How to do this in a

Memory efficient way for list comprehension of pandas dataframe using multiple columns

北慕城南 提交于 2020-06-09 04:05:39
问题 I want to run a function on rows of a pandas dataframe in list comprehension. Dataframe can have varying number of columns. How to make use these columns of dataframe? import pandas as pd df = {'chrom': ['chr1', 'chr1','chr1'], 'start': [10000, 10100, 12000], 'end':[10150,10120,12250], 'S1':[1, 1, 1],'S2':[2, 2, 2],'S3':[3, 3, 3] } df = pd.DataFrame(data=df) print(df) def func(row): print(row) [func(row) for row in zip(df['chrom'],df['start'],df['S1'],df['S2'],df['S3'])] How to do this in a

python: combine 2 ordered lists into list of tuples

狂风中的少年 提交于 2020-06-08 14:25:20
问题 I have 2 lists that I want to combine into a single list of tuples, so that order is maintained and the result[i] is (first[i], second[i]) . Assume that the two lists will always be of the same size. Is there a way to do this using list comprehension? So for example: >>> first = [1,2,3] >>> second = [4,5,6] >>> combine(first, second) [(1,4), (2,5), (3,6)] I've tried a few things [(i,j) for i in first, j in second] [(i for i in first, j for j in second)] [(i,j) for i,j in first, second] None

list comprehension in pandas

扶醉桌前 提交于 2020-06-08 08:19:22
问题 I'm giving a toy example but it will help me understand what's going on for something else I'm trying to do. Let's say I want a new column in a dataframe 'optimal_fruit' that is apples * orange - bananas. I can do something like this to get it. df2['optimal_fruit'] = df2['apples'] * df2['oranges'] - df2['bananas'] apples oranges bananas optimal_fruit 1 6 11 -5 2 7 12 2 3 8 13 11 4 9 14 22 5 10 15 35 What is happening if I try to do something like this? And how could I do this in a list

list comprehension in pandas

别来无恙 提交于 2020-06-08 08:19:18
问题 I'm giving a toy example but it will help me understand what's going on for something else I'm trying to do. Let's say I want a new column in a dataframe 'optimal_fruit' that is apples * orange - bananas. I can do something like this to get it. df2['optimal_fruit'] = df2['apples'] * df2['oranges'] - df2['bananas'] apples oranges bananas optimal_fruit 1 6 11 -5 2 7 12 2 3 8 13 11 4 9 14 22 5 10 15 35 What is happening if I try to do something like this? And how could I do this in a list

list comprehension in pandas

我的未来我决定 提交于 2020-06-08 08:19:08
问题 I'm giving a toy example but it will help me understand what's going on for something else I'm trying to do. Let's say I want a new column in a dataframe 'optimal_fruit' that is apples * orange - bananas. I can do something like this to get it. df2['optimal_fruit'] = df2['apples'] * df2['oranges'] - df2['bananas'] apples oranges bananas optimal_fruit 1 6 11 -5 2 7 12 2 3 8 13 11 4 9 14 22 5 10 15 35 What is happening if I try to do something like this? And how could I do this in a list

list comprehensions with break

风流意气都作罢 提交于 2020-05-16 02:35:08
问题 I have the following code that I would like to write in one line with a list comprehension. list1 = [4, 5, 6, 9, 10, 16, 21, 23, 25, 27] list2 = [1, 3, 5, 7, 8, 11, 12, 13, 14, 15, 17, 20, 24, 26, 56] list3 = [] for i in list1: for j in list2: if j>i: # print(i,j) list3.append(j) break print(list1) print(list3) The output is: [4, 5, 6, 9, 10, 16, 21, 23, 25, 27] [5, 7, 7, 11, 11, 17, 24, 24, 26, 56] It's the break statement that throws me off, I don't know where to put it. Thank you 回答1: You

list comprehensions with break

假装没事ソ 提交于 2020-05-16 02:35:03
问题 I have the following code that I would like to write in one line with a list comprehension. list1 = [4, 5, 6, 9, 10, 16, 21, 23, 25, 27] list2 = [1, 3, 5, 7, 8, 11, 12, 13, 14, 15, 17, 20, 24, 26, 56] list3 = [] for i in list1: for j in list2: if j>i: # print(i,j) list3.append(j) break print(list1) print(list3) The output is: [4, 5, 6, 9, 10, 16, 21, 23, 25, 27] [5, 7, 7, 11, 11, 17, 24, 24, 26, 56] It's the break statement that throws me off, I don't know where to put it. Thank you 回答1: You

Updating object properties in list comprehension way

老子叫甜甜 提交于 2020-05-12 11:25:36
问题 Is that possible, in Python, to update a list of objects in list comprehension or some similar way? For example, I'd like to set property of all objects in the list: result = [ object.name = "blah" for object in objects] or with map function result = map(object.name = "blah", objects) Could it be achieved without for-looping with property setting? (Note: all above examples are intentionally wrong and provided only to express the idea) 回答1: Ultimately, assignment is a "Statement", not an

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

蹲街弑〆低调 提交于 2020-05-01 05:08:15
问题 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