我是python的新手!在下面,我列出了5个不同的字符串。
afilelistawarded = ['123,456,789,12345,Correct,67890','a123,b456,c789,d12345,Correct,e67890','f123,g456,h789,i12345,Correct,j67890','k123,l456,m789,n12345,Correct,o67890','p123,q456,r789,s12345,Correct,t67890']
for i1 in afilelistawarded:
for i2 in (afilelistawarded[i1]):
del ((afilelistawarded[i1])[0])
del ((afilelistawarded[i1])[1])
del ((afilelistawarded[i1])[2])
del ((afilelistawarded[i1])[3])
del ((afilelistawarded[i1])[5])
print afilelistawarded
我正在尝试输出以下内容:
afilelistawarded=['Correct','Correct','Correct','Correct','Correct']
如何删除不需要的项目并将正确的字符串输出到同一列表?
答案 0 :(得分:0)
如果确定“正确”总是出现在index=4
上,则只需使用:
afilelistawarded = ['123,456,789,12345,Correct,67890','a123,b456,c789,d12345,Correct,e67890','f123,g456,h789,i12345,Correct,j67890','k123,l456,m789,n12345,Correct,o67890','p123,q456,r789,s12345,Correct,t67890']
for i1 in afilelistawarded:
print i1.split(",")[4]
答案 1 :(得分:0)
这对我有用:
>>> afilelistawarded = ['123,456,789,12345,Correct,67890','a123,b456,c789,d12345,Correct,e67890','f123,g456,h789,i12345,Correct,j67890','k123,l456,m789,n12345,Correct,o67890','p123,q456,r789,s12345,Correct,t67890']
>>> [i.split()[4] for i in afilelistawarded]
['Correct', 'Correct', 'Correct', 'Correct', 'Correct']
答案 2 :(得分:0)
在这里,如果您确定“正确”元素的位置,这应该可以工作。
ls = []
filelistawarded = ['123,456,789,12345,Correct,67890','a123,b456,c789,d12345,Correct,e67890','f123,g456,h789,i12345,Correct,j67890','k123,l456,m789,n12345,Correct,o67890','p123,q456,r789,s12345,Correct,t67890']
for item in afilelistawarded:
ls.append(item.split(',')[4])