3
under__list =[[74, 0.1423287845938018, None, None, 10, 
   1.9099604642265018, 0.5185563065935468, 1.6825659992347914, 
   3.547506695574544, 2.7789822726693023, 20051, 0, 25, None, ' >50K'],
  [44, 0.9181229773462783, None, None, 14, 0.17973300970873787, 
   0.1644822006472492, 0.13940129449838187, 1.1252427184466018, 
   0.4357200647249191, 0, 0, 40, None, ' <=50K']]

I have the above list but I want to add the elements together but skip past the None and the ">=50" elements.

I want to be able to do it even if I don't know where None number values are. Any suggestions?

for item in zip(under__list[row]):
    under__list.append(int(sum(item[0])))

Looking for the below output:

[1182, 25.2452245, None, None, 9212, 256, 2624, 25.24,
   2532, 25, 2005252, 52, 25632, None, ' >50K']

It would be one list with numbers added together.

1
  • 3
    Please provide an example of the desired output for this input; in particular, it isn't clear what adding you wish to do. Commented Nov 23, 2014 at 17:33

3 Answers 3

1

It looks like you want to sum the items on the same indices from all inner lists. For that first of all you need to use zip with *, and then in the list comprehension check of the first item of each row is an instance of Number type or simply int or float(isinstance(x[0], (int, float))), if yes, sum them else use the first items as value.

>>> from numbers import Number
>>> [sum(x) if isinstance(x[0], Number) else x[0] for x in zip(*under__list)]
[118, 1.0604517619400802, None, None, 24, 2.0896934739352395, 0.683038507240796, 1.8219672937331732, 4.672749414021146, 3.2147023373942214, 20051, 0, 65, None, ' >50K']

The expression sum(x) if isinstance(x[0], Number) else x[0] is called a conditional expression.

Sign up to request clarification or add additional context in comments.

Comments

1
for item in under_list:
    item_ = filter(lambda x: x != None and x >= 50, under_list)
    # Here compute sum of item_ and append to the right place

Comments

1

Write a function to return what you need:

def foo(thing):
    try:
        return sum(thing)
    except TypeError:
        return thing[0]

map the function to the zipped rows of under__list

>>> under__list =[[74, 0.1423287845938018, None, None, 10, 1.9099604642265018, 0.5185563065935468, 1.6825659992347914, 3.547506695574544, 2.7789822726693023, 20051, 0, 25, None,' >50K'], [44, 0.9181229773462783, None, None, 14, 0.17973300970873787, 0.1644822006472492, 0.13940129449838187, 1.1252427184466018, 0.4357200647249191, 0, 0, 40, None, ' <=50K']]

>>> map(foo, zip(*under__list))
[118, 1.0604517619400802, None, None, 24, 2.0896934739352395, 0.683038507240796, 1.8219672937331732, 4.672749414021146, 3.2147023373942214, 20051, 0, 65, None, ' >50K']
>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.