Extract numbers from an irregular list of lists

This is the place for queries that don't fit in any of the other categories.

Extract numbers from an irregular list of lists

Postby pythonforever » Wed Feb 05, 2014 6:57 pm

Was just wandering what would be the simplest way to extract numbers only from a list and sublists?

would it be easier using re or not?
numbers = ['a', 3, 'b', 5, [8, 9, 'c', 'd'], 11, 'e',['f', 15]]

Thank you.
Last edited by Yoriz on Wed Feb 05, 2014 8:26 pm, edited 1 time in total.
Reason: Changed title.
pythonforever
 
Posts: 9
Joined: Thu Dec 05, 2013 7:18 pm

Re: Extract numbers from list

Postby stranac » Wed Feb 05, 2014 7:13 pm

What silly way using re did you have in mind?
Also, will there only be one level of nesting?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Extract numbers from list

Postby pythonforever » Wed Feb 05, 2014 7:22 pm

Not sure what you meant by silly way.
Just how to extract all numbers from that one list
pythonforever
 
Posts: 9
Joined: Thu Dec 05, 2013 7:18 pm

Re: Extract numbers from list

Postby stranac » Wed Feb 05, 2014 7:32 pm

pythonforever wrote:Not sure what you meant by silly way.

Ok, then. What non-silly way using re did you have in mind?
pythonforever wrote:Just how to extract all numbers from that one list

Code: Select all
extracted_numbers = [3, 5, 8, 9, 11, 15]
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Extract numbers from list

Postby pythonforever » Wed Feb 05, 2014 7:36 pm

Thanks, That was awesome code.
pythonforever
 
Posts: 9
Joined: Thu Dec 05, 2013 7:18 pm

Re: Extract numbers from list

Postby stranac » Wed Feb 05, 2014 7:41 pm

Ok, then...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Extract numbers from list

Postby pythonforever » Wed Feb 05, 2014 7:45 pm

To be honest, I dont know if you were playing with me with your answers or not but I am truly sorry i asked.
pythonforever
 
Posts: 9
Joined: Thu Dec 05, 2013 7:18 pm

Re: Extract numbers from list

Postby stranac » Wed Feb 05, 2014 7:50 pm

I wasn't playing with you.

I asked for more information:
stranac wrote:What silly way using re did you have in mind?
Also, will there only be one level of nesting?

You replied:
pythonforever wrote:Just how to extract all numbers from that one list


If you only need to do it for that one list, then the best way is to just use a list which contains what you actually want.
If you need to do it for more than just that, give some more details.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm

Re: Extract numbers from list

Postby Yoriz » Wed Feb 05, 2014 8:07 pm

Using answer from stackoverflow to flatten the irregular list of lists.
Code: Select all
import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el,
                basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

Code: Select all
from numbers import Number

numbers = ['a', 3, 'b', 5, [8, 9, 'c', 'd'], 11, 'e', ['f', 15]]

Code: Select all
print filter(lambda x: isinstance(x, Number), flatten(numbers))
or
Code: Select all
print [item for item in flatten(numbers) if isinstance(item, Number)]


Note: use itertools.ifilter if its a large list.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests