得到i和j条件匹配的行

2024-05-15 02:45:32 发布

您现在位置:Python中文网/ 问答频道 /正文

打印每个输出一次:

 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

Tags: andtoinforoutputiff1pl
1条回答
网友
1楼 · 发布于 2024-05-15 02:45:32

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

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') 

相关问题 更多 >

    热门问题