Reputation: 554
I have a dataset which the pdb debugger tells me looks like this;
set1 : [(4.4, ), (4.8, ), (4.2, ), (4.0, ), (4.2, ), (4.8, ), (2.0, ), (2.4, ), (3.0, )]
I need it to be in the format below;
set2 : [4.4, 4.8, 4.2, 4.0, 4.2, 4.8, 2.0, 2.4, 3.0]
What types, or variations of types, are set 1 and 2 and how do you cast, or otherwise, set 1 into set 2.
I'm new to python and required to do this in a rush so there's no time for me to learn in the detail I should just now - any help will be greatly appreciated.
Thanks!
Upvotes: 1
Views: 85
Reputation: 63777
The data looks very familiar and deserves a treatment with zip
print list(zip(*set1)[0])
Upvotes: 0
Reputation: 27702
Try with:
set1 = [(4.4, ), (4.8, ), (4.2, ), (4.0, ), (4.2, ), (4.8, ), (2.0, ), (2.4, ), (3.0, )]
set2 = map(lambda x: x[0], set1)
set1
is a list of one-element tuples, map
returns the result of applying the function passed as first parameter to the collection passed as second parameter.
In this answer, that function is the one that returns the first element of a tuple.
A more pythonic way to do this is to use list comprehension:
set2 = [x[0] for x in set1]
Upvotes: 1
Reputation: 8702
Try using itertools.chain
from itertools import chain
set1 =[(4.4, ), (4.8, ), (4.2, ), (4.0, ), (4.2, ), (4.8, ), (2.0, ), (2.4, ), (3.0, )]
print list(chain.from_iterable(set1))
#output [4.4, 4.8, 4.2, 4.0, 4.2, 4.8, 2.0, 2.4, 3.0]
Upvotes: 0