summing only the numbers contained in a list

前端 未结 6 1754
猫巷女王i
猫巷女王i 2020-12-17 20:27

Give a method that sums all the numbers in a list. The method should be able to skip elements that are not numbers. So, sum([1, 2, 3]) should be

相关标签:
6条回答
  • 2020-12-17 21:00

    You can do this with a simple one liner:

    l1 = [1, 2, 3, 'A']
    
    sum(filter(lambda i: isinstance(i, int), l1))
    # prints 6
    

    Or, if you need it inside a function:

    def foo(l1):
        return sum(filter(lambda i: isinstance(i, int), l1))
    

    Additionally, as noted in the comments, don't use names like dict and list for your variables; *they will shadow they build-in names for the dictionary (dict) and (list) types. You'll then need to explicitly del dict, list in order to use them as intended.


    But, let me explain. What filter does is here is:

    a) It takes a function as its first argument:

    # this function will return True if i is an int
    # and false otherwise
    lambda i: isinstance(i, int)
    

    and then takes every element inside the list l1 (second argument) and evaluates whether it is True or False based on the function.

    b) Then, filter will essentially filter out any objects inside list l1 that are not instances of int (i.e the function returns False for them). As a result, for a list like [1, 2, 3, 'A'] filter is going to return [1, 2, 3] which will then be summed up by sum().

    Some Examples:

    foo([1, 2, 3, 'A'])
    # 6
    
    foo([1, 2, 3])
    # 6
    
    foo([1, 2, 3, 'HELLO', 'WORLD'])
    # 6
    

    Slight caveat:

    As is, this doesn't sum up float values, it drops them (and any other numeric types for that case). If you need that too, simply add the float type in the lambda function as so:

    lambda i: isinstance(i, (int, float))
    

    Now, your function sums floats too:

    foo([1, 2, 3, 3.1,  'HELLO', 'WORLD'])
    # 9.1
    

    Add any other types as necessary in the lambda function to catch the cases that you need.


    A catch all case:

    As noted by @Copperfield you can check for objects that are instances of any number by utilizing the numbers.Number abstract base class in the numbers module. This acts as a catch-all case for numeric values:

    import numbers # must import
    sum(filter(lambda i: isinstance(i, numbers.Number), l1))
    

    Simpler and a bit faster, too:

    Additionally, as noted by @ShadowRanger, and since lambda might not be the most comfortable construct for new users, one could simply use a generator expression (which is also faster) with sum to get the same exact result:

    sum(val for val in l1 if isinstance(val, numbers.Number))
    
    0 讨论(0)
  • 2020-12-17 21:00

    The Pythonic way is to do a try/except. While you could do this in a one liner, I prefer to break things out a bit to see exactly what is happening.

    val=0
    for item in list:
        try:
            val+=int(item)
        except ValueError:
            pass
    

    If you want to include floating points, simply change the int to a float. Floating points are anything with a decimal, among others.

    0 讨论(0)
  • 2020-12-17 21:00
    sum([x for x in list if isinstance(x, (int, long, float))])
    
    0 讨论(0)
  • 2020-12-17 21:04

    use filter and isinstance like this

    >>> test = [1,2,3,4,5,6,"A","B"]
    >>> sum(filter(lambda x:isinstance(x,int),test))
    21
    >>> 
    
    0 讨论(0)
  • 2020-12-17 21:06
    def foo(list):
    dict= "ABCDEFGHIJKLMN"
    n=0
    for i in range(0,len(list)-1):
        if str(list[i]) in dict:
            ""
        else:    
            n= n+list[i]
    return n
    print foo([1,2,3,4,5,6,"A","B"])
    
    0 讨论(0)
  • 2020-12-17 21:09
    def filtersum(L):
        if not L: return 0
        if not isinstance(L[0], int): return filtersum(L[1:])
        return L[0] + filtersum(L[1:])
    

    Output:

    In [28]: filtersum([1,2,3])
    Out[28]: 6
    
    In [29]: filtersum([1,'A', 2,3])
    Out[29]: 6
    
    0 讨论(0)
提交回复
热议问题