我有这个清单。我想创建一个for
循环,该循环将在列表中这些文件的功能组合中使用。
我不确定如何进行这些组合,以便对每个“检查”都采用正确的组合。
该函数,如果不是用于循环,则将如下所示:
erase('check3_dwg_Polyline','check3_dwg_Polyline_feat_to_polyg_feat_to_line','output_name')
我尝试过的事情:
这是列表。
li=['check3_dwg_Polyline', 'check2_dwg_Polyline',
'check3_dwg_Polyline_feat_to_polyg',# this will not be needed to extracted
'check2_dwg_Polyline_feat_to_polyg',# >> >>
'check3_dwg_Polyline_feat_to_polyg_feat_to_line',
'check2_dwg_Polyline_feat_to_polyg_feat_to_line']
从此开始:
a=[li[i:i+3] for i in range(0, len(li), 3)]
其中返回:
[['check3_dwg_Polyline',
'check2_dwg_Polyline',
'check3_dwg_Polyline_feat_to_polyg'],
['check2_dwg_Polyline_feat_to_polyg',
'check3_dwg_Polyline_feat_to_polyg_feat_to_line',
'check2_dwg_Polyline_feat_to_polyg_feat_to_line']]
最后:
for base, base_f, base_line in a:
print(base, base_line, base + "_output")
给予:
check3_dwg_Polyline check3_dwg_Polyline_feat_to_polyg check3_dwg_Polyline_output
check2_dwg_Polyline_feat_to_polyg check2_dwg_Polyline_feat_to_polyg_feat_to_line check2_dwg_Polyline_feat_to_polyg_output
其他方法:
base = [f for f in li if not f.endswith(("_polyg", "_to_line"))]
base_f = {f.strip("_feat_to_polyg"): f for f in li if f.endswith("_polyg")}
base_line = {f.strip("_feat_to_polyg_feat_to_line"): f for f in li if f.endswith("_to_line")}
[(b, base_f[b], base_line[b]) for b in base]
给予:
KeyError: 'check3_dwg_Polyline'
我已经尝试过对列表进行排序,但是经过上述过程后,它只是以不同的方式破坏了列表。
理想的结果是
尝试时:
for base, base_f, base_line in a:
print(base, base_line, base + "_output")
为此:
check3_dwg_Polyline check3_dwg_Polyline_feat_to_polyg_feat_to_line check3_dwg_Polyline_output
check2_dwg_Polyline check2_dwg_Polyline_feat_to_polyg_feat_to_line check2_dwg_Polyline_output
将放在这样的地方:
erase('check3_dwg_Polyline','check3_dwg_Polyline_feat_to_polyg_feat_to_line','output_name')
答案 0 :(得分:0)
zip将该列表分成 check3 , check2 块……然后即可进行 for 循环。
n = len(li) // 3
a = zip(*[li[i:i+n] for i in range(0, len(li), n)])
({pprint(list(a))
将输出
[('check3_dwg_Polyline',
'check3_dwg_Polyline_feat_to_polyg',
'check3_dwg_Polyline_feat_to_polyg_feat_to_line'),
('check2_dwg_Polyline',
'check2_dwg_Polyline_feat_to_polyg',
'check2_dwg_Polyline_feat_to_polyg_feat_to_line')]