开发者

python - appending different columns from a file to different lists?

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 :-)


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']]


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜