0

我有一个文件,其中包含以下内容:

IA45,TA43,WB58,AB71 ... etc

我想将这些单词转换为列表,例如:

["IA45","TA43","WB58","AB71" .....]

然后,我想用这个列表一个接一个地取单词并拆分并将第一个字母、第二个字母和数字作为分隔变量。我有:

with open("IRL.txt", "r") as f:
    resid_list = [x.split(',') for x in f.readlines()]
    for r in resid_list:
        chain = list(r)[1]
        print chain
        res = list(r)[0]
        print res
        num = list(r)[2:]
        num1 = "".join(num)
        res_num = res+num1
        print res_num+" "+chain

问题出在 for 循环中。当我打印“链”时,它只给了我第二个词,没有别的 ---> TA43

但我想打印 --- A

4

3 回答 3

2

I think you want something like the following:

with open("IRL.txt", "r") as f:
    resid_list = [x for line in f.readlines() for x in line.split(',')]
    for r in resid_list:
        first = r[0]
        second = r[1]
        num = r[2:]
        # do something with those variables

In your current version resid_list is a list of lists, but I think you want a flat list of strings.

于 2013-06-18T19:35:36.343 回答
1

可以使用re.findall生成器表达式并获取第一个匹配项

import re

line = 'IA45,TA43,WB58,AB71'
for fst, snd, nums in (re.findall('(.)(.)(\d+)', el)[0] for el in line.split(',')):
    print fst, snd, nums

I A 45
T A 43
W B 58
A B 71

根据您的行的格式/可预测程度,您甚至可以侥幸逃脱:

for fst, snd, nums in re.findall('(.)(.)(\d+)', line):
    print fst, snd, nums
于 2013-06-18T19:46:12.180 回答
1

变成for r in resid_list_for r in resid_list[0]

由于您使用的是列表理解,因此您的参数如下所示:

In [9]: resid_list
Out[9]: [['IA45', 'TA43', 'WB58', 'AB71']]

即列表列表

In [10]: resid_list[0]
Out[10]: ['IA45', 'TA43', 'WB58', 'AB71']

给你你想要的。

于 2013-06-18T19:32:44.917 回答