我有一个包含子列表k = [ ]
的列表。
我写了一个片段,我在其中列出了子列表k[0]
和k[1]
,我进行了比较,得到了out = [ ]
的新列表:
for i in range(len(k[0])):
for j in range(len(k[1])):
r = list(chain(k[0][i],k[1][j]))
j = [i for i, _ in groupby(r)]
if len(j) < len(r):
out.append(j)
完成上述代码后,我希望以这种方式替换列表k
:
k = [out, k[2:]]
然后for
循环即将再次执行,但使用新值k[0]
和k[1]
,直到子列表在k[2:]
中用完
我尝试过&#39;而#39;并且&#39;尝试&#39;,但现在结果不好。
这是要测试的更大代码:
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from itertools import chain, combinations, groupby
df = pd.read_csv("graph.csv", sep=';', encoding='utf-8')
g = nx.Graph()
g = nx.from_pandas_dataframe(df, 'nodes_1', 'nodes_2')
plt.show()
a1 = pd.Series([0] + df['nodes_2'][df['nodes_2']%2==0].values.tolist())
a2 = pd.Series(df['nodes_2'][df['nodes_2']%2==1].values.tolist())
b1, b2 = a1.apply(g.neighbors), a2.apply(g.neighbors)
c1, d1 = a1.values.tolist(), b1.values.tolist()
c2, d2 = a2.values.tolist(), b2.values.tolist()
e00=[]
for i in range(len(c1)):
for j in range(len(d1[i])):
if d1[i][j]%2==0:
d = [c1[i],d1[i][j]]
e00.append(d)
e01=[]
for i in range(len(c1)):
for j in range(len(d1[i])):
if d1[i][j]%2==1:
d = [c1[i],d1[i][j]]
e01.append(d)
e10=[]
for i in range(len(c2)):
for j in range(len(d2[i])):
if d2[i][j]%2==0:
d = [c2[i],d2[i][j]]
e10.append(d)
e11=[]
for i in range(len(c2)):
for j in range(len(d2[i])):
if d2[i][j]%2==1:
d = [c2[i],d2[i][j]]
e11.append(d)
a00 = pd.DataFrame(e00)
a01 = pd.DataFrame(e01)
a10 = pd.DataFrame(e10)
a11 = pd.DataFrame(e11)
a = input('Write a binary number. \n')
b=[]
for i in range(len(a)-1):
c = a[i:i+2]
b.append(c)
print (b, '\n')
dct = {'00' : e00, '01' : e01, '10' : e10, '11' : e11}
k=[]
for i in b:
y = dct[i]
k.append(y)
out=[]
for i in range(len(k[0])):
for j in range(len(k[1])):
r = list(chain(k[0][i],k[1][j]))
j = [i for i, _ in groupby(r)]
if len(j) < len(r):
out.append(j)
print (k, '\n')
print (out, '\n')
和graph.csv:
nodes_1 nodes_2
0 0 1
1 0 2
2 1 3
3 1 4
4 2 5
5 2 6
6 3 7
7 3 8
8 4 9
9 4 10
10 5 11
11 5 12
12 6 13
13 6 14
我想做什么?
输入样本二进制字符串后的:001101
,我得到分配给字典的值:['00', '01', '11', '10', '01']
。在每个字母a00, a01, a10, a11
中都是相同的数据。我的循环需要数据匹配&#39; 00&#39;并将其与&01; 01&#39;下的数据进行比较。列表out
已创建。如您所见,仍未使用值"11", "10", "01"
。因此,out
列表将检索下一个值以进行比较,并执行与以前相同的操作。直到疲惫['00', '01', '11', '10', '01']
。
我正在考虑替换k = [out,k [2:]],以便仍然使用相同的循环。
答案 0 :(得分:1)
如果它适合你,你可以尝试这种方法,这不是一个确切的解决方案,你可以从中得到帮助,提示。 当你注释到你想要2-2 chuck但是在一次浏览之后你想要那个chuck来代替那两个项目:
first for itertation ==>> ['00', '01'] = out_list now origional list become [[out_list], '11', '10', '01']
for second iteration ==>> [[out_list], '11'] = out_list now origional list become [[out_list],'10', '01']
for third iteration ==>> [[out_list],10] = out_list now origional list become [[out_list],'01]
for forth iteration ==>> [[out_list],'01'] = out_list now origional list become [[out_list]]
这是方法:
actual_list=['00', '01', '11', '10', '01']
def group(updated_list,actual_list):
print(updated_list) #This is just for showing output
exit_condition = []
for item in updated_list:
if not isinstance(item,list):
exit_condition.append(item)
if not exit_condition:
return 0
else:
out = []
for index in range(0, len(updated_list), 2):
k = updated_list[index:index + 2]
#do you stuff with k[0] and k[1] before inserting to out list
out.insert(len(out), k)
for item in actual_list:
out.insert(len(updated_list), item)
updated_list = out
return group(updated_list,actual_list[1:])
print(group(actual_list,actual_list[2:]))
输出:
['00', '01', '11', '10', '01']
[['00', '01'], '11', '10', '01']
[[['00', '01'], '11'], '10', '01']
[[[['00', '01'], '11'], '10'], '01']
[[[[['00', '01'], '11'], '10'], '01']]