0

Trying to remove single quotes from around numbers. I'm working with third paty data that's not well typed.

lst =  [ ('text','2','3','4'), ('text2','4','5','6') ]
y=  [map(int,i) for i in zip(*lst)[1:]] 

d = zip(*list)[0]
print d
c= zip(*y)
print c

dd = zip(d,c)
print dd

this is what the out is:

('text', 'text2')
[(2, 3, 4), (4, 5, 6)]
[('text', (2, 3, 4)), ('text2', (4, 5, 6))]

How Do I get:

dd =  [ ('text',2,3,4), ('text2',4,5,6) ]

EDIT: If list is sometimes this: [ ['text','2','3','4'], ['text2','4','5','6'] ], then what do i do? Another problem is integer as '3,400'.

New Lst example:

  lst =  [ ('text','2','3','4'), ('text2','4','5,000','6,500') ]

Need:

 [ ('text',2,3,4), ('text2',4,5000,6500) ]
1
  • What is 3,400' supposed to mean? 3, 3400 or 3.4 ? Commented Feb 12, 2011 at 20:03

4 Answers 4

8
print [(text, int(a), int(b), int(c)) for (text, a, b, c) in lst]
Sign up to request clarification or add additional context in comments.

5 Comments

damn it, I thought it was harder.
if list is sometimes this: lst = [ ['text','2','3','4'], ['text2','4','5','6'] ]. then would do i do?
@user428862: It doesn't matter what kind the sequence is, just the number of items in it is important. So you use the same code.
@user428862: if you have commas in the numbers, then you can change the int(a) type calls to int(a.replace(',','')). That's assuming that 3,333 means 3333 in your numbers.
@user428862: print [(text, int(a), int(b.replace(',','')), int(c.replace(',',''))) for (text, a, b, c) in lst]
2

Jochen's answer is the right thing for your specific case.

If, for some reason, you need to take the list of types as a parameter you can do something like this:

>>> lst =  [ ('text','2','3','4'), ('text2','4','5','6') ]

>>> def map_rows(types, rows):
...     return [tuple(f(x) for f, x in zip(types, row)) for row in rows]

>>> map_rows((str, int, int, int), lst)
[('text', 2, 3, 4), ('text2', 4, 5, 6)]

The map_rows defined above is sort of a cousin of the standard map function. Note that "types" is really a sequence of callables that "convert" the value in whatever way you want.

Comments

1
lst = [('text','2','3','4'), ('text2','4','5','6')]
dd = []
for t in lst:
    new_tuple = []
    for i in t:
        try:
            new_tuple.append(int(i))
        except ValueError:
            new_tuple.append(i)
    dd.append(tuple(new_tuple))

Comments

0

lst =  [ ('text','2','3','4'), ('text2','4','5','6') ]

new_lst = []

for tup in lst:
    tmp = []
    for index, item in enumerate(tup):
        if index != 0:
            tmp.append(int(item))
        else:
            tmp.append(item)
    new_lst.append(tuple(tmp))

print new_lst

This is probably not the pythonic way of doing it :)

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.