在构成图表的代码中,我有四个变量具有相似条件的块。
区块 1:
a =''.join(map(str, ['a1' if x%4==0 else 'a2' if x%4==1 else 'a3' if x%4==2 else 'a3' if x%4==3 else 'a4' for x in comb_3bit[i]]))
区块 2:
good=[]
all=[]
for j in range(comb_3bit_without[0].count('a1')):
k = j*4
all.append(k)
good.append(k)
for j in range(kombinacje_3bit_bez_powtorzen[0].count('a2')):
k = j*4 + 1
all.append(k)
good.append(k)
for j in range(kombinacje_3bit_bez_powtorzen[0].count('a3')):
k = j*4 + 2
all.append(k)
good.append(k)
for j in range(kombinacje_3bit_bez_powtorzen[0].count('a4')):
k = j*4 + 3
all.append(k)
good.append(k)
区块 3:
if len([i for i in good2 if i%4==0 ])==0:
good2.insert(0,0)
if len([i for i in good2 if i%4==1 ])==0:
good2.insert(1,1)
if len([i for i in good2 if i%4==2 ])==0:
good2.insert(2,2)
if len([i for i in good2 if i%4==3 ])==0:
good2.insert(3,3)
第 4 块:
p0 = [i for i in good2 if i%4 ==0 ]
p1 = [i for i in good2 if i%4 ==1 ]
p2 = [i for i in good2 if i%4 ==2 ]
p3 = [i for i in good2 if i%4 ==3 ]
z0 = p0[-1]
z1 = p1[-1]
z2 = p2[-1]
z3 = p3[-1]
区块 5:
g.add_node(z0+h*4)
g.add_node(z1+h*4)
g.add_node(z2+h*4)
g.add_node(z3+h*4)
good2.append(z0+h*4)
good2.append(z1+h*4)
good2.append(z2+h*4)
good2.append(z3+h*4)
区块 6:
a1 = [i for i in good2 if i%4 ==0 ]
a2 = [i for i in good2 if i%4 ==1 ]
a3 = [i for i in good2 if i%4 ==2 ]
a4 = [i for i in good2 if i%4 ==3 ]
sources = {
'a1': a1,
'a2': a2,
'a3': a3,
'a4': a4,
}
但是,现在我想概括一下上述片段,以便所有代码都可以轻松用于更多变量a[i]
。我将不胜感激任何帮助和指导。