For循环不会附加到列表中

时间:2016-12-02 15:17:11

标签: python list append

我正在尝试创建一个包含列表的for循环,并根据内容添加不同的字符串。这就是我所拥有的:

for person in hugelist:
    if person[4]=="[b]" and person[5]=="[b]" in person:
        person.append("tn")
    elif person[4] == "[b]" and person[5]== "[n]" in person:
        person.append("fp")
    elif person[4] == "[n]" and person[5]== "[b]" in person:
        person.append("fn")
    elif person[4] == "[n]" and person[5] == "[n]" in person:
        person.append("tp")
    else:
        person.append("N/A")

所有这一切都是N/A附加到所有列表,如果我禁用最后一个,它根本不附加任何内容。我做错了什么?

我正在使用的文件的示例文本:

 [['1', 'R2_3_3_2_0Y0.pct', '1355', '[n]', '[n]', '0'],
 ['2', 'R3_4_3_2_90Y150.pct', '2079', '[n]', '[n]', '150'],
 ['3', '2_4_4_2_0Y150.pct', '1834', '[b]', '[b]', '150'],
 ['4', '3_3_3_2_0Y100.pct', '4780', '[b]', '[b]', '100'],
 ['5', '3_3_3_3_90Y50.pct', '1685', '[b]', '[b]', '50'],
 ['6', 'R2_4_4_2_0Y50.pct', '1237', '[n]', '[n]', '50']]

我希望它使用"tn"[b], [b]添加到列表中,依此类推。

2 个答案:

答案 0 :(得分:1)

列表使用从索引0开始的索引。

random_list=['0_index','1_index','2_index']

如果您更改if..elif中的索引并移除in person,那么它就可以正常工作。

hugelist=[['1', 'R2_3_3_2_0Y0.pct', '1355', '[n]', '[n]', '0'],
 ['2', 'R3_4_3_2_90Y150.pct', '2079', '[n]', '[n]', '150'],
 ['3', '2_4_4_2_0Y150.pct', '1834', '[b]', '[b]', '150'],
 ['4', '3_3_3_2_0Y100.pct', '4780', '[b]', '[b]', '100'],
 ['5', '3_3_3_3_90Y50.pct', '1685', '[b]', '[b]', '50'],
 ['6', 'R2_4_4_2_0Y50.pct', '1237', '[n]', '[n]', '50']]

for person in hugelist:
    if person[3]=="[b]" and person[4]=="[b]":
        person.append("tn")
    elif person[3] == "[b]" and person[4]== "[n]":
        person.append("fp")
    elif person[3] == "[n]" and person[4]== "[b]":
        person.append("fn")
    elif person[3] == "[n]" and person[4] == "[n]":
        person.append("tp")
    else:
        person.append("N/A")

hugelist将成为:

[['1', 'R2_3_3_2_0Y0.pct', '1355', '[n]', '[n]', '0', 'tp'],
['2', 'R3_4_3_2_90Y150.pct', '2079', '[n]', '[n]', '150', 'tp'],
['3', '2_4_4_2_0Y150.pct', '1834', '[b]', '[b]', '150', 'tn'],
['4', '3_3_3_2_0Y100.pct', '4780', '[b]', '[b]', '100', 'tn'],
['5', '3_3_3_3_90Y50.pct', '1685', '[b]', '[b]', '50', 'tn'],
['6', 'R2_4_4_2_0Y50.pct', '1237', '[n]', '[n]', '50', 'tp']]

答案 1 :(得分:0)

请注意,列表的索引以0开头。

hugelist =  [['1', 'R2_3_3_2_0Y0.pct', '1355', '[n]', '[n]', '0'],
             ['2', 'R3_4_3_2_90Y150.pct', '2079', '[n]', '[n]', '150'],
             ['3', '2_4_4_2_0Y150.pct', '1834', '[b]', '[b]', '150'],
             ['4', '3_3_3_2_0Y100.pct', '4780', '[b]', '[b]', '100'],
             ['5', '3_3_3_3_90Y50.pct', '1685', '[b]', '[b]', '50'],
             ['6', 'R2_4_4_2_0Y50.pct', '1237', '[n]', '[n]', '50']]

for person in hugelist:
    if person[3]=="[b]" and person[4]=="[b]":
        person.append("tn")
    elif person[3] == "[b]" and person[4]== "[n]":
        person.append("fp")
    elif person[3] == "[n]" and person[4]== "[b]":
        person.append("fn")
    elif person[3] == "[n]" and person[4] == "[n]":
        person.append("tp")
    else:
        person.append("N/A")

print(hugelist)

输出,

[['1', 'R2_3_3_2_0Y0.pct', '1355', '[n]', '[n]', '0', 'tp'], 
['2', 'R3_4_3_2_90Y150.pct', '2079', '[n]', '[n]', '150', 'tp'], 
['3', '2_4_4_2_0Y150.pct', '1834', '[b]', '[b]', '150', 'tn'], 
['4', '3_3_3_2_0Y100.pct', '4780', '[b]', '[b]', '100', 'tn'], 
['5', '3_3_3_3_90Y50.pct', '1685', '[b]', '[b]', '50', 'tn'], 
['6', 'R2_4_4_2_0Y50.pct', '1237', '[n]', '[n]', '50', 'tp']]