【发布时间】:2018-05-02 06:39:16
【问题描述】:
我正在尝试编写一种算法,在该算法中搜索可能的节点路径,表示二进制字符串。其中偶数节点对应数字“0”,奇数节点对应数字“1”。以下代码暂时不优雅且未优化。在代码 cmets 中我对他的行为做了一些解释。
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("graph.csv", sep=';', encoding='utf-8')
df1=df.astype(int)
g = nx.Graph()
g = nx.from_pandas_dataframe(df1, 'nodes_1', 'nodes_2')
plt.show()
# I load any binary string.
# Example '01'
z = input('Write a binary number. \n')
z1=list(z)
l1 = df1['nodes_2'].tolist()
# I add to the list '0', because in df1 ['nodes_2'] the node '0' is missing.
l1[:0] = [0]
# I check whether the first digit entered in the input() of the variable 'z' is 0 or 1.
# And with good values I create a list of 'a'.
a=[]
if int(z1[0])==0:
for i in l1:
if i%2==0:
num1 = int(i)
a.append(num1)
elif int(z1[0])==1:
for i in l1:
if i%2 ==1:
num1 = int(i)
a.append(num1)
else: print('...')
# I am creating 'b' list of neighbors lists for nodes from list 'a'.
b=[]
c=[]
for i in a:
c.append(i)
x4 = g.neighbors(i)
b.append(x4)
# For neighbors I choose only those that are odd in this case,
# because the second digit from the entered 'z' is 1,
# and then I create a list of 'e' matching pairs representing the possible graph paths.
e=[]
if int(z1[1])==0:
for j in range(len(b)):
for k in range(len(b[j])):
if b[j][k]%2==0:
d = [a[j], b[j][k]]
e.append(d)
elif int(z1[1])==1:
for j in range(len(b)):
for k in range(len(b[j])):
if b[j][k]%2==1:
d = [a[j], b[j][k]]
e.append(d)
print (a)
# Output:
# [0, 2, 4, 6, 8, 10, 12, 14]
print (b)
# Output:
# [[1, 2], [0, 5, 6], [1, 9, 10], [2, 13, 14], [3], [4], [5], [6]]
print (e)
# Output:
# [[0, 1], [2, 5], [4, 1], [4, 9], [6, 13], [8, 3], [12, 5]]
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
目前,我在调整用于任何长二进制字符串的代码时遇到问题。因为在上面的例子中只能使用 2 位字符串。因此,我将非常感谢有关简化和自定义代码的任何提示。
【问题讨论】:
标签: python algorithm list pandas networkx