-3

I have the following list in python

a = ['[', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', '0', ',', ' ', '1', ',', ' ', '1', ',', ' ', '0', ']'].

I want to extract integers form this, so that I can get a new list [1,0,0,0,0,1,0,1,1,0]. I am using python 3.x and I have tried the solution given in the link How to extract numbers from a list of strings? but unfortunately I am getting an empty list []. Can somebody help me?

user3483203
  • 50,081
  • 9
  • 65
  • 94

4 Answers4

7

Use isdigit with a list comprehension:

[int(i) for i in a if i.isdigit()]

# [1, 0, 0, 0, 0, 1, 0, 1, 1, 0]
user3483203
  • 50,081
  • 9
  • 65
  • 94
1

Someone messed up this data really bad. We have the following:

a = ['[', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', '0', ',', ' ', '1', ',', ' ', '1', ',', ' ', '0', ']']

Let's try something

b = ''.join(a)

Now b is '[1, 0, 0, 0, 0, 1, 0, 1, 1, 0]'. This looks like a task for literal_eval:

import ast
ast.literal_eval(b)

Et voilà, the list is back: [1, 0, 0, 0, 0, 1, 0, 1, 1, 0].

To put everything together:

import ast

a = ['[', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', ',', ' ', '0', ',', ' ', '1', ',', ' ', '1', ',', ' ', '0', ']']
result = ast.literal_eval(''.join(a))

Whoever created this mess of data used a combination of castings to string and list. Check for yourself: print(str(list(str([1, 0, 0, 0, 0, 1, 0, 1, 1, 0]))))

Matthias
  • 12,873
  • 6
  • 42
  • 48
0

If list comprehensions like

t = [int(x) for x in a if x.isdigit()]

and

t = [int(g) for g in filter(lambda x: x.isdigit(),a)] 

are beyond you, you can use a simpler approach. Iterate through all items in your source list, try to convert it into an integer, add the result to a list.

If errors occure, it is not an integer and you ignore that element and do not add it to the result list:

a = ['[', '1', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '0', ',', ' ', '1', 
     ',', ' ', '0', ',', ' ', '1', ',', ' ', '1', ',', ' ', '0', ']']

rv = []

for text in a:
    try:
        rv.append(int(text))   # appennd if convertable to int,
    except: # catchall 
        pass                   # else pass

print(rv)

Output:

[1, 0, 0, 0, 0, 1, 0, 1, 1, 0]

ReadUp:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-2

A more efficient solution:

list(map(int, filter(str.isdigit, a)))

Please read Python List Comprehension Vs. Map. map is faster than list comprehension when dealing with non-lambda functions.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • More efficient? This is slower than the list comprehension. – user3483203 Jun 27 '18 at 19:53
  • On a list `10000 * a`: `In [373]: %timeit [int(i) for i in a if i.isdigit()] 35.4 ms ± 259 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)` – user3483203 Jun 27 '18 at 19:53
  • `In [374]: %timeit list(map(int, filter(str.isdigit, a))) 39.7 ms ± 310 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)` – user3483203 Jun 27 '18 at 19:53
  • @user3483203 Not sure what kind of computer you have, but on my laptop, with the same a * 10000 setup, `python -mtimeit "list(map(int, filter(str.isdigit, a)))"` yields `10 loops, best of 3: 70.7 msec per loop`, while `python -mtimeit "[int(i) for i in a if i.isdigit()]"` yields `10 loops, best of 3: 79.4 msec per loop`. – blhsing Jun 27 '18 at 20:46
  • @user3483203 Please read: https://stackoverflow.com/a/1247490/6890912 `map` is more efficient than list comprehension when dealing with non-lambda functions. – blhsing Jun 27 '18 at 21:00