two for loops in list comprehension python
问题 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