如何在python中过滤列表

时间:2017-06-28 17:24:48

标签: python python-2.7

如何过滤我的列表,以便创建一个单独的整数列表,以便稍后将其添加。

我的尝试:

dList=  [['A1', 24],    ['A2',  22],    ['A3',  12],    ['A4',  20],    ['A5',  40],    ['A6',  62],    ['A7',  80],    ['A8',  12],    ['B1',  145],   ['B2',  127],   ['B3',  26],    ['B4',  25],    ['B5',  91],    ['B6',  38],    ['B7',  21],    ['B8',  12],    ['C1',  122],   ['C2',  87],    ['C3',  36],    ['C4',  3], ['D1',  0], ['D2',  5], ['D3',  55],    ['D4',  62],    ['D5',  98],    ['D6',  32]]

def totalWales(dList):
    sum = 0
    numList = filter(operator.isNumberType, dList)
    for x in numList:
        sum = sum + int(numList)
    print "Total wales: ", numList

6 个答案:

答案 0 :(得分:0)

您可以使用列表推导创建所有子列表索引1元素的新列表:

dList= [['A1', 24],
        ['A2', 22],
        ['A3', 12],
        ['A4', 20],
        ['A5', 40],
        ['A6', 62],
        ['A7', 80],
        ['A8', 12],
        ['B1', 145],
        ['B2', 127],
        ['B3', 26],
        ['B4', 25],
        ['B5', 91],
        ['B6', 38],
        ['B7', 21],
        ['B8', 12],
        ['C1', 122],
        ['C2', 87],
        ['C3', 36],
        ['C4', 3],
        ['D1', 0],
        ['D2', 5],
        ['D3', 55],
        ['D4', 62],
        ['D5', 98],
        ['D6', 32]]

newlist = [x[1] for x in dList]

print (newlist)

打印:

[24, 22, 12, 20, 40, 62, 80, 12, 145, 127, 26, 25, 91, 38, 21, 12, 122, 87, 36, 3, 0, 5, 55, 62, 98, 32]

在不创建新列表的情况下求和:

sumDlist = sum(x[1] for x in dList)

print (sumDlist)

打印:

1257

如果你需要在求和之前转换数字(请注意,如果它不是数字,我的上述解决方案将会失败):

sumDlist = sum(float(x[1]) for x in dList))

额外:

如果您想快速查看数字,这个模块很棒:http://fastnumbers.readthedocs.io/en/stable/api.htmlhttps://pypi.python.org/pypi/fastnumbers

并像这样使用:

from fastnumbers import *

dList= [['A1', 24],
        ['A2', 22],
        ['A3', 12],
        ['A4', 20],
        ['A5', 40],
        ['A6', 62],
        ['A7', 80],
        ['A8', 12],
        ['B1', 145],
        ['B2', 127],
        ['B3', 26],
        ['B4', 25],
        ['B5', 91],
        ['B6', 38],
        ['B7', 21],
        ['B8', 12],
        ['C1', 122],
        ['C2', 87],
        ['C3', 36],
        ['C4', 3],
        ['D1', 0],
        ['D2', 5],
        ['D3', 55],
        ['D4', 62],
        ['D5', 98],
        ['D6', 32]]

sum_of_dList = 0
for sublist in dList:
    if isreal(sublist[1]):
        sum_of_dList += fast_real(sublist[1])
print (sum_of_dList)

打印:

1257

答案 1 :(得分:0)

你可以使用一些zip魔法:

dList= [['A1', 24], ['A2', 22], ['A3', 12], ['A4', 20], ['A5', 40], ['A6', 62], ['A7', 80], ['A8', 12], ['B1', 145], ['B2', 127], ['B3', 26], ['B4', 25], ['B5', 91], ['B6', 38], ['B7', 21], ['B8', 12], ['C1', 122], ['C2', 87], ['C3', 36], ['C4', 3], ['D1', 0], ['D2', 5], ['D3', 55], ['D4', 62], ['D5', 98], ['D6', 32]]
numbers = zip(*dList)[1]
sum(numbers)

这里zip选择每个列表中的第一个和第二个元素并将它们放在单独的列表中,然后索引[1]将获得数字。

答案 2 :(得分:0)

如果dlist始终采用所示格式,您可以通过执行以下操作获取整数列表:

intlist = [i[1] for i in dlist]

这将从每个子列表的第二个元素创建一个新列表。

可以使用相同的列表推导语法直接计算出你所追求的总和:

intsum = sum (i[1] for i in dlist)

答案 3 :(得分:0)

如果你只关心总和,那么就这样做

>>> sum(num for _,num in dList)
1257

顺便说一句,

operator.isNumberType(obj)

  

自版本2.7以后不推荐使用:改为使用isinstance(x, numbers.Number)

答案 4 :(得分:0)

#Create a list with the numbers:
numList = [numbers for letters, numbers in dList]

#Set the result of the sum o the numbers to a variable:
result = sum(numList)

#Or if you just need the sum of the numbers:
result = sum([numbers for letters, numbers in dList])

答案 5 :(得分:-1)

我是这样做的

dList=  [['A1', 24],    ['A2',  22],    ['A3',  12],    ['A4',  20],    ['A5',  40],    ['A6',  62],    ['A7',  80],    ['A8',  12],    ['B1',  145],   ['B2',  127],   ['B3',  26],    ['B4',  25],    ['B5',  91],    ['B6',  38],    ['B7',  21],    ['B8',  12],    ['C1',  122],   ['C2',  87],    ['C3',  36],    ['C4',  3], ['D1',  0], ['D2',  5], ['D3',  55],    ['D4',  62],    ['D5',  98],    ['D6',  32]]

def totalWales(dList):
    sum = 0
    numList = []
    for x in range(0, len(dList)):
        for y in range(0, len(dList[x])):
            if type(dList[x][y]) == int:
                sum += dList[x][y]
                numList.append(dList[x][y])
    print "Total wales: ", numList

totalWales(dList)

我遍历了dList中的每个项目,然后是该数组中的每个项目。我检查了该项是否为int。如果是int,则求和并添加到numList