从字符串数组列表中抓取值?

dis*_*dia 1 python list maya

如何选择/获取字符串数组列表中的值?

当我执行代码来读取我的文件中的内容时,它如下:

for line in testFile:
test = line.split()

#Output:
['1', '21', '32', '43', '54', '65', '76', '87']
['2', '31', '42', '53', '64', '75', '86', '97']
['3', '41', '52', '63', '74', '85', '96', '107']
...
...
Run Code Online (Sandbox Code Playgroud)

但是,现在据说我想选择并获取第一个值 - 1,2,3仅在输出中,我得到['1', '21', '32', '43', '54', '65', '76', '87']或最后一行的值,我应该编码print test[0]还是for item in test..

这意味着,如果我决定抓住第3列的值,它会给我32,42,52,如果我抓住第6列,它会给我65,75,85等等.列数是相同的,我问这个因为我要么去设置旋转/平移属性中的值,而第一列是帧编号...

有可能这样做吗?

the*_*eye 5

您只需要使用zip函数转置内容,就像这样

with open("inputfile") as f:
    rows = zip(*[line.split() for line in f])

print rows[0]
Run Code Online (Sandbox Code Playgroud)

我们用openwith声明打开文件.然后,我们逐行读取并拆分每条读取线.所以,我们得到一个列表清单.现在,我们将每个列表应用于zip函数,实际上将它们转换为它们.转置后,行成为列,列成为行.例如,

[[1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)

会变成

[[1, 4, 7]
 [2, 5, 8]
 [3, 6, 9]]
Run Code Online (Sandbox Code Playgroud)