获取i和j条件匹配的行

时间:2018-07-26 10:19:24

标签: python loops

每个输出打印一次:

 f1=['part3_pl', 
     'part2_pl', 
     'part3_pl_to_p', 
     'part2_pl_to_p', 
     'part3_pl_to_p_lack', 
     'part2_pl_to_p_lack']


for i in f1:
    for j in f1:
        if i.endswith('pl') and j.endswith('pl_to_p'):
            print(i,j,i+'_output')

返回:

part3_pl part3_pl_to_p part3_pl_output
part3_pl part2_pl_to_p part3_pl_output
part2_pl part3_pl_to_p part2_pl_output
part2_pl part2_pl_to_p part2_pl_output

我想每行打印一次。

正确的输出:

part3_pl part3_pl_to_p part3_pl_output
part2_pl part2_pl_to_p part2_pl_output

1 个答案:

答案 0 :(得分:2)

请再次检查输出。根据您的代码,输出应为:

for i in f1:
 for j in f1:
    if i.endswith('pl') and j.endswith('pl_to_p'):
        print(i,j,i+'_output') 

输出:

part3_pl part3_pl_to_p part3_pl_output
part3_pl part2_pl_to_p part3_pl_output
part2_pl part3_pl_to_p part2_pl_output
part2_pl part2_pl_to_p part2_pl_output

您想要的输出可以通过以下代码实现:

for i in f1:
 for j in f1:
  if i.endswith('pl') and j==i+ '_to_p':
    print(i,j,i+'_output')