简体   繁体   English

在python中创建和访问列表列表

[英]Creating and accessing list of lists in python

I am bit new to python. 我对python有点陌生。 I started today. 我今天开始。 My code looks like this as follows 我的代码如下所示

testcases=[
(([0.5,0.4,0.3],'HHTH'),[0.4166666666666667, 0.432, 0.42183098591549295, 0.43639398998330553]),
(([0.14,0.32,0.42,0.81,0.21],'HHHTTTHHH'),[0.5255789473684211, 0.6512136991788505, 0.7295055220497553, 0.6187139453483192, 0.4823974597714815, 0.3895729901052968, 0.46081730193074644, 0.5444108434105802, 0.6297110187222278]),
(([0.14,0.32,0.42,0.81,0.21],'TTTHHHHHH'),[0.2907741935483871, 0.25157009005730924, 0.23136284577678012, 0.2766575695593804, 0.3296000585271367, 0.38957299010529806, 0.4608173019307465, 0.5444108434105804, 0.6297110187222278]),
(([0.12,0.45,0.23,0.99,0.35,0.36],'THHTHTTH'),[0.28514285714285714, 0.3378256513026052, 0.380956725493104, 0.3518717367468537, 0.37500429586037076, 0.36528605387582497, 0.3555106542906013, 0.37479179323540324]),
(([0.03,0.32,0.59,0.53,0.55,0.42,0.65],'HHTHTTHTHHT'),[0.528705501618123, 0.5522060353798126, 0.5337142767315369, 0.5521920592821695, 0.5348391689038525, 0.5152373451083692, 0.535385450497415, 0.5168208803156963, 0.5357708613431963, 0.5510509656933194, 0.536055356823069])]

print 'Inputs'
print '======'
for inputs,output in testcases:
    print inputs[0]

print 'Outputs'
print '======='
for inputs,output in testcases:
    print output[0]

In the above code gives output as follows 在上面的代码中给出了如下输出

Inputs 输入项

======
[0.5, 0.4, 0.3]
[0.14, 0.32, 0.42, 0.81, 0.21]
[0.14, 0.32, 0.42, 0.81, 0.21]
[0.12, 0.45, 0.23, 0.99, 0.35, 0.36]
[0.03, 0.32, 0.59, 0.53, 0.55, 0.42, 0.65]
Outputs
=======
0.416666666667
0.525578947368
0.290774193548
0.285142857143
0.528705501618

But I need to access each row of the testcases list above like to get the output as follows what should be the code? 但是我需要访问上面的测试用例列表的每一行,以获取输出,如下所示,代码应该是什么?

([0.5,0.4,0.3],'HHTH'),[0.4166666666666667, 0.432, 0.42183098591549295, 0.43639398998330553])

Seems like you need a nested loop for that. 似乎您需要一个嵌套循环。 eg: 例如:

for inputs,output in testcases:
    for output in outputs:
        print output

This will print what you're after (if that's what you mean). 这将打印出您想要的内容(如果这就是您的意思)。

>>> for inputs,outputs in testcases:
...     print '%r, %r' % (inputs, outputs)

([0.5, 0.4, 0.3], 'HHTH'), [0.4166666666666667, 0.432, 0.42183098591549295, 0.43639398998330553]
([0.14, 0.32, 0.42, 0.81, 0.21], 'HHHTTTHHH'), [0.5255789473684211, 0.6512136991788505, 0.7295055220497553, 0.6187139453483192, 0.4823974597714815, 0.3895729901052968, 0.46081730193074644, 0.5444108434105802, 0.6297110187222278]
([0.14, 0.32, 0.42, 0.81, 0.21], 'TTTHHHHHH'), [0.2907741935483871, 0.25157009005730924, 0.23136284577678012, 0.2766575695593804, 0.3296000585271367, 0.38957299010529806, 0.4608173019307465, 0.5444108434105804, 0.6297110187222278]
([0.12, 0.45, 0.23, 0.99, 0.35, 0.36], 'THHTHTTH'), [0.28514285714285714, 0.3378256513026052, 0.380956725493104, 0.3518717367468537, 0.37500429586037076, 0.36528605387582497, 0.3555106542906013, 0.37479179323540324]
([0.03, 0.32, 0.59, 0.53, 0.55, 0.42, 0.65], 'HHTHTTHTHHT'), [0.528705501618123, 0.5522060353798126, 0.5337142767315369, 0.5521920592821695, 0.5348391689038525, 0.5152373451083692, 0.535385450497415, 0.5168208803156963, 0.5357708613431963, 0.5510509656933194, 0.536055356823069]

And if you really require the extra ) at the end of the line change the print statement to: 如果您确实需要在行尾添加) ,请将print语句更改为:

print '%r, %r)' % (inputs, outputs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM