列表理解与列表中元素的总和

List Comprehension with sum of elements in a list

我有一个如下所示的列表:

[['State', 'Gas', 'Air', 'Food', 'Party'],
 ['Alabama', 4802982, 9, 213068.0, 52419.02],
 ['Alaska', 721523, 3, 31618.0, 663267.26],
 ['Arizona', 6412700, 11, 144393.0, 113998.3],
 ['Arkansas', 2926229, 6, 209159.0, 53178.62],
 ['California', 37341989, 55, 394608.0, 163695.57],
 ['Colorado', 5044930, 9, 184289.0, 104093.57],
 ['Connecticut', 3581628, 7, 45744.0, 5543.33],
 ['Delaware', 900877, 3, 13849.0, 2489.27],
 ['District of Columbia', 0, 3, 3418.0, 68.34],
 ['Florida', 18900773, 29, 271024.0, 65754.59],
 ['Georgia', 9727566, 16, 271920.0, 59424.77],
 ['Hawaii', 1366862, 4, 9662.0, 10930.98],
 ['Idaho', 1573499, 4, 98649.0, 83570.08],
 ['Illinois', 12864380, 20, 305872.0, 57914.38]]

我想创建一个名为 total 的列表,其中按以下顺序包含数据元素的总和:各州的天然气总和、各州的空气总和、各州的食品总和以及各州的政党总和.显然避免了列表顶部的列名列表以及州名。我尝试了多种方法,但到目前为止,我认为这是引导我朝着正确方向前进的方法:

total = [sum(x) for x in statesData[x]]

如果可以使用numpy,一行代码即可解决

data = [['State', 'Gas', 'Air', 'Food', 'Party'],
 ['Alabama', 4802982, 9, 213068.0, 52419.02],
 ['Alaska', 721523, 3, 31618.0, 663267.26],
 ['Arizona', 6412700, 11, 144393.0, 113998.3],
 ['Arkansas', 2926229, 6, 209159.0, 53178.62],
 ['California', 37341989, 55, 394608.0, 163695.57],
 ['Colorado', 5044930, 9, 184289.0, 104093.57],
 ['Connecticut', 3581628, 7, 45744.0, 5543.33],
 ['Delaware', 900877, 3, 13849.0, 2489.27],
 ['District of Columbia', 0, 3, 3418.0, 68.34],
 ['Florida', 18900773, 29, 271024.0, 65754.59],
 ['Georgia', 9727566, 16, 271920.0, 59424.77],
 ['Hawaii', 1366862, 4, 9662.0, 10930.98],
 ['Idaho', 1573499, 4, 98649.0, 83570.08],
 ['Illinois', 12864380, 20, 305872.0, 57914.38]]

sum_states = np.sum(np.array(data)[1:,1:].T.astype(np.float16),axis=1)

要用 list-comprehension 解决这个问题,转置 map(list,zip(*data)) 的列表列表将是 good idea

[sum(item[1:]) for item in list(map(list, zip(*data)))[1:]]

使用 zipnext 的一种方式:

it = zip(*statesData[1:])
next(it) # Pop out the state names

["Total", *(sum(i) for i in it)]

输出:

['Total', 106165938, 179, 2197273.0, 1436348.0800000003] 

Obviously avoiding the column name list at the top of the list and also the states' names.

所以,首先摆脱那些:

numbers = [row[1:] for row in data[1:]]

sum of the data elements [columnwise]

所以我们需要做的第一件事是transpose the data交换列和行,然后我们可以对每一行求和。

transposed = # go check the other answer!
total_gas, total_air, total_food, total_party = [sum(column) for column in transposed]
# Alternate spelling:
# total_gas, total_air, total_food, total_party = map(sum, transposed)
# This works because our function only needs one argument, which is an element
# from the transposed list; and because we are unpacking the resulting `map`
# object right away.

但您的一般潜在问题似乎真的是“我如何理解列表理解?”。在这种情况下,请参阅 this reference