1

I am using python and facing problem to extract specific elements from string which has tuples consisting of numbers. One thing to note here is tuples of numbers in the strings are not fixed, it can be more or less. The format of string will be same as mentioned below:

'string = [(100, 1), (2500, 2), (5000, 3), (10000, 3).....]'

Desired output:

[100,2500,5000,10000.....]

What I tried:

So far I tried splitting above string to get following output

['string', '=', '(100', '1)', '(2500', '2)', '(5000', '3)', '(10000, '3)']

and after that I was planning to strip unwanted characters like (,' to get numbers I want but this method has to be hard coded for every tuple and the length of tuples in string is not fixed.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Radheya
  • 779
  • 1
  • 11
  • 41

4 Answers4

3

You can split your text with = to find the list of tuples then use ast.literaleval() to evaluate your list:

>>> next(zip(*literal_eval(s.split('=')[-1].strip())))
(100, 2500, 5000, 10000)

Note that since in python 2.X zip returns a list you can use indexing to get the first item:

zip(*literal_eval(s.split('=')[-1].strip()))[0]
(100, 2500, 5000, 10000)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
2

Use re.findall

>>> s = 'string = [(100, 1), (2500, 2), (5000, 3), (10000, 3).....]'
>>> print [int(i) for i in re.findall(r'\((\d+)', s)]
[100, 2500, 5000, 10000]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Dec 01 '15 at 10:06
1

You can ast.literal_eval:

>>> import ast
>>> my_str = 'string = [(100, 1), (2500, 2), (5000, 3), (10000, 3)]'
>>> [x[0] for x in ast.literal_eval(my_str.split("=")[-1].strip())]
[100, 2500, 5000, 10000]
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • Hackaholic - I got the solution from Kasramvd's post which also uses `literal_eval`. Thanks anyways – Radheya Dec 01 '15 at 10:11
0
>>> s = 'string = [(100, 1), (2500, 2), (5000, 3), (10000, 3)]'
>>> l = [i[0] for i in eval(s.split("=")[1])]
>>> l
[100, 2500, 5000, 10000]
Mayur Koshti
  • 1,794
  • 15
  • 20
  • Mayur - I think due to safety reasons it is suggested to use `literal_eval` instead of simple `eval`. I once read it here: http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – Radheya Dec 01 '15 at 10:20