1

How can I create from this (two columns, fixed width):

0.35    23.8
0.39    23.7
0.43    23.6
0.47    23.4
0.49    23.1
0.51    22.8
0.53    22.4
0.55    21.6

Two lists:

list1 = [0.35, 0.39, 0.43, ...]
list2 = [23.8, 23.7, 23.6, ...]

Thank you.

0

2 Answers 2

16

Probably you are looking for something like this

>>> str1 = """0.35    23.8
0.39    23.7
0.43    23.6
0.47    23.4
0.49    23.1
0.51    22.8
0.53    22.4
0.55    21.6"""
>>> zip(*(e.split() for e in str1.splitlines()))
[('0.35', '0.39', '0.43', '0.47', '0.49', '0.51', '0.53', '0.55'), ('23.8', '23.7', '23.6', '23.4', '23.1', '22.8', '22.4', '21.6')]

You can easily extend the above solution to cater to any type of iterables including file

>>> with open("test1.txt") as fin:
    print zip(*(e.split() for e in fin))


[('0.35', '0.39', '0.43', '0.47', '0.49', '0.51', '0.53', '0.55'), ('23.8', '23.7', '23.6', '23.4', '23.1', '22.8', '22.4', '21.6')]

Instead of strings if you want the numbers as floats, you need to pass it through the float function possibly by map

>>> zip(*(map(float, e.split()) for e in str1.splitlines()))
[(0.35, 0.39, 0.43, 0.47, 0.49, 0.51, 0.53, 0.55), (23.8, 23.7, 23.6, 23.4, 23.1, 22.8, 22.4, 21.6)]

And finally to unpack it to two separate lists

>>> from itertools import izip
>>> column_tuples = izip(*(map(float, e.split()) for e in str1.splitlines()))
>>> list1, list2 = map(list, column_tuples)
>>> list1
[0.35, 0.39, 0.43, 0.47, 0.49, 0.51, 0.53, 0.55]
>>> list2
[23.8, 23.7, 23.6, 23.4, 23.1, 22.8, 22.4, 21.6]

So how it works

zip takes a list of iterables and returns a list of pair wise tuple for each iterator. itertools.izip is similar but instead of returning a list of pairwise tuples, it returns an iterator of pairwise tuples. This would be more memory friendly

map applied a function to each element of the iterator. So map(float, e.split) would convert the strings to floats. Note an alternate way of representing maps is through LC or generator expression

Finally str.splitlines converts the newline separated string to a list of individual lines.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1969754: If the answer helped, accept it and try upvoting
0

Try this:

splitted = columns.split()
list1 = splitted[::2] #column 1
list2 = splitted[1::2] #column 2

3 Comments

First you cannot slice a generator. Second this will not return the exact result, you probably meant splitted = column.split(). Third , the slice should actually be written splitted[::2] and splitted[1::2], Fourth may not work with all form of iterables, port your solution to use islice
thanks for the corrections. I didn't have an interpreter on my machine to test the code. Every day is a school day.
If you don't have Python in your local machine you can IDEONE it

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.