list-comprehension

two for loops in list comprehension python

让人想犯罪 __ 提交于 2021-01-29 08:16:18
问题 I have a list: f_array=['1000,1','100,10','100,-10'] I am trying to sum up all the first element in each value of the above array. I tried something like: number = sum([ num for num in item.split(",")[1] for item in f_array]) but it dint work. What would be the best way to do it ? 回答1: If you want to use nested loops then need to swap the order of the for loops: number = sum([num for item in f_array for num in item.split(",")[1]]) List comprehension loops are listed in nesting order , left to

List comprehensions with class objects

与世无争的帅哥 提交于 2021-01-29 08:09:26
问题 I have a class named StrucData in subfile.py class StrucData: def __init__(self, name): self.name=name def loadData(self, size=1, cost=1): self.size=size self.cost=cost In the main file I: call the subfile, create a list of data names loop through the list to instantiate the objects; and load data using 'loadData' method for each object (I'm using the same 'size' and 'cost' to make this example easy.) from subfile import StrucData listIndex=['data1','data2','data3'] # Create a list of objects

create nested dict from pandas dataframe

天涯浪子 提交于 2021-01-29 04:03:09
问题 I have a pandas dataframe that I would like to pull information from and create a nested dictionary for downstream use, however, I'm not very good at working with pandas yet and I could use some help! My dataframe looks something like this: Sequence A_start A_stop B_start B_stop 0 sequence_1 1 25 26 100 1 sequence_2 1 31 32 201 2 sequence_3 1 27 28 231 3 sequence_4 1 39 40 191 I want to write this to a dictionary so that it has this form: d = {‘Sequnce: {(‘A_start’, ‘A_stop’) : [{'repeat

How to apply a list comprehension in Panda Dataframe?

情到浓时终转凉″ 提交于 2021-01-29 02:44:32
问题 From a list of values, I try to identify any sequential pair of values whose sum exceeds 10 a = [1,9,3,4,5] ...so I wrote a for loop... values = [] for i in range(len(a)-2): if sum(a[i:i+2]) >10: values += [a[i:i+2]] ...which I rewritten as a list comprehension... values = [a[i:i+2] for i in range(len(a)-2) if sum(a[i:i+2]) >10] Both produce same output: values = [[1,9], [9,3]] My question is how best may I apply the above list comprehension in a DataFrame. Here is the sample 5 rows DataFrame

How to apply a list comprehension in Panda Dataframe?

泪湿孤枕 提交于 2021-01-29 02:21:30
问题 From a list of values, I try to identify any sequential pair of values whose sum exceeds 10 a = [1,9,3,4,5] ...so I wrote a for loop... values = [] for i in range(len(a)-2): if sum(a[i:i+2]) >10: values += [a[i:i+2]] ...which I rewritten as a list comprehension... values = [a[i:i+2] for i in range(len(a)-2) if sum(a[i:i+2]) >10] Both produce same output: values = [[1,9], [9,3]] My question is how best may I apply the above list comprehension in a DataFrame. Here is the sample 5 rows DataFrame

How does `[ (x !! 0, x !! 1) | x <- mapM (const ['A', 'B', 'C'] ) [1..2], head x < head (tail x) ]` work?

∥☆過路亽.° 提交于 2021-01-28 17:56:36
问题 I am new to Haskell and wondering how the statement [ (x !! 0, x !! 1) | x <- mapM (const ['A', 'B', 'C'] ) [1..2], head x < head (tail x) ] works. (I found it on StackOverflow.) I know what it outputs, but I am not really understanding it. 回答1: Well the above expression is likely not what is considered idiomatic Haskell. Probably a better version would be: [ (x0, x1) | (x0:x1:_) <- mapM (const "ABC") [1..2], x0 < x1 ] This is cleaner and if the lists in the mapM (const "ABC") would return a

How to enumerate items in a dictionary with enumerate( ) in python

孤街浪徒 提交于 2021-01-28 09:36:54
问题 As the title suggests I wanted to enumerate the key and its values (without brackets) in python. I tried the following code : example_dict = {'left':'<','right':'>','up':'^','down':'v',} [print(i,j,a) for (i,j,a) in enumerate(example_dict.items())] But it doesn't work. I want the output to be like this 0 left < 1 right > 2 up ^ 3 down v Thank you in advance 回答1: In this case enumerate returns (index, (key, value)) , so you just need to change your unpacking to for i, (j, a) , though

How to convert this my code into a list comprehension

a 夏天 提交于 2021-01-28 08:24:08
问题 I've written this code so that it generates 4 random ints ranging from 1-6 and then remove the smallest number and add it to a list that is returned. I was reading around and found that list comprehensions are the more "pythonic" solution instead of these small for range loops. I would like to know how to write this code as a list comprehension and any help would be greatly appreciated. stats = [] for stat in range(6): score = [random.randint(1, 6) for n in range(4)] score.remove(min(score))

Value Count with List in New Column that Comprised it Pandas [duplicate]

五迷三道 提交于 2021-01-28 08:13:15
问题 This question already has answers here : Multiple aggregations of the same column using pandas GroupBy.agg() (3 answers) Closed 9 months ago . I have a dataframe with individuals who called a variety of numbers. As so: Person Called A 123 B 123 C 234 I need to create a new dataframe that makes a list of people who called that number and the count. Like this: Persons Called Count A, B 123 2 C 234 1 I'm pretty sure I can just create a for loop that counts the number of times and appends them to

list comprehension creating nested lists

限于喜欢 提交于 2021-01-28 04:37:06
问题 I'd like to create nested lists of days per month list per year list: [[ 31, 29, 31, 30 ], [ 31, 28, 31, 30 ] ] with mm = [ 1, 2, 3, 4 ] yy = [ 2012, 2013 ] but my code: [ [ result.append( calendar.monthrange( y, m )[ 1 ] ) for m in mm] for y in yy ] produces: [31, 29, 31, 30, 31, 28, 31, 30 ] Can someone please tell me what I've done wrong? Thanks. BSL 回答1: So, I'm assuming the full code looks something like this: result = [] [ [ result.append( calendar.monthrange( y, m )[ 1 ] ) for m in mm]