2

I have a tab-delimited file as below:

A    3    A    6
B    6    B    9
C    0    C    2

I wish to read the file in as below:

LIST = [['A', '3'], ['B', '6'], ['C', '0'], ['A', '6'], ['B', '9'], ['C', '2']]

The order is not important. I am only concerned that each row is read in increments of two and assigned to a sub list.

Any suggestions?

Thanks, S :-)

2 Answers 2

3

The most straightforward way would be:

>>> n = []
>>> for line in open(fname):
    els = line.split('\t')
    n.append(els[:2])
    n.append(els[2:])


>>> n
[['A', '3'], ['A', '6'], ['B', '6'], ['B', '9'], ['C', '0'], ['C', '2']]

maybe slightly more efficient would be:

>>> g = (line.split('\t') for line in open(fname))
>>> [els[i:i+2] for els in g for i in range(0, 4, 2)]
[['A', '3'], ['A', '6'], ['B', '6'], ['B', '9'], ['C', '0'], ['C', '2']]
Sign up to request clarification or add additional context in comments.

1 Comment

eeeewww :). there should be a better way to do this. At least say (0,2) instead of that range(0,4,2). Also if you do just split() (no arguments), it will split on space, tab, new lines... you can just do g = open(fname).read().split()
0

This question actually allows for some Pythonic solutions.

l = open(fname).read().split()
LIST = zip(l[::2], l[1::2])

That's all there is to it. "Bam!"

Or we can "knock it up a notch":

def couple(i):
    while True:
        yield (i.next(), i.next())

LIST = [w for w in couple(iter(open(fname).read().split()))]

Additional benefits of those are that they will read the file in pairs, no matter if there are 4 columns (as in the example) or 6, 2... whatever


See also pythonic-way-to-split-comma-separated-numbers-into-pairs and how-do-you-split-a-list-into-evenly-sized-chunks-in-python

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.