Python 中的邻接列表创建

Adjacency list creation in Python

我正在尝试使用包含以下内容的文本文件来实现邻接表:

0,1,5,1,3,1,0,3,1,1,0,4,7,1,0,4,0,4,1,6,5,1,1,3,6,3,4,1,5,2
6,0,1,4,1,5,3,0,4,5,1,4,7,4,2,0,3,5,4,1,3,1,7,3,2,1,1,6,5,0
4,6,0,3,1,6,1,6,2,1,2,2,7,7,7,1,1,1,6,1,1,5,1,6,1,6,1,2,5,3
6,4,5,0,4,1,6,1,6,2,1,0,6,5,1,3,0,1,0,7,5,1,6,6,3,2,3,1,6,4
4,2,0,6,0,0,4,6,3,3,3,6,7,0,1,4,5,7,3,4,2,3,7,7,1,4,6,6,4,5
0,2,6,1,6,0,3,3,6,1,4,4,3,5,0,7,3,7,3,7,0,2,7,1,7,2,5,7,1,0
7,4,5,2,2,2,0,3,1,4,0,6,0,3,7,1,4,6,6,6,6,1,1,6,1,3,3,3,2,0
2,0,1,3,1,7,2,0,3,0,5,2,3,4,1,7,1,0,1,5,3,6,1,2,2,0,5,2,4,0
7,3,2,6,2,6,6,1,0,1,0,6,2,3,0,5,2,1,7,0,0,3,3,0,1,1,1,4,6,1
4,1,7,4,2,5,7,1,4,0,4,3,6,1,6,6,1,0,4,0,0,0,2,2,4,2,6,0,7,5
1,1,2,1,0,5,1,6,2,6,0,4,4,6,2,5,1,1,0,0,5,0,1,1,2,5,1,3,6,6
0,3,7,3,1,4,6,1,2,7,1,0,4,4,1,1,6,7,1,5,0,4,0,4,6,3,1,4,1,0
1,4,7,0,4,1,5,3,1,0,0,6,0,0,5,0,3,5,4,7,2,1,4,7,3,1,5,7,3,2
3,1,5,3,3,4,2,4,4,5,1,7,6,0,3,3,0,0,7,7,3,5,6,7,0,2,1,0,2,2
3,1,4,4,1,5,1,5,6,2,6,4,5,7,0,0,4,1,3,7,5,1,0,4,5,4,5,1,1,0

想想每一行都有索引号作为名称而不是 A、B、C.... 所以例如 0,1,5,1,3,1,0,3,1,1,0,4,7,1,0,4,0,4,1,6,5,1,1,3,6,3,4,1,5,2 是节点 06,0,1,4,1,5,3,0,4,5,1,4,7,4,2,0,3,5,4,1,3,1,7,3,2,1,1,6,5,0 是节点 1 等。这里的问题是我无法提取元素,因为每个元素之间有“,”。我试过这个:

a = np.array([])
with open("routers.txt") as f:
    adj_list = list(f)
    node_no = 0
    node_list = list()

    for i in adj_list:

        node_list.append(node_no)
        if i[node_no] != ",":
          a = np.append(a, i[node_no])
          node_no += 1
        elif i[node_no] == ",":
          break

但是它不能再前进了,当它看到“,”它就停止了。

您可以使用拆分:

text = """0,1,5,1,3,1,0,3,1,1,0,4,7,1,0,4,0,4,1,6,5,1,1,3,6,3,4,1,5,2
6,0,1,4,1,5,3,0,4,5,1,4,7,4,2,0,3,5,4,1,3,1,7,3,2,1,1,6,5,0
4,6,0,3,1,6,1,6,2,1,2,2,7,7,7,1,1,1,6,1,1,5,1,6,1,6,1,2,5,3
6,4,5,0,4,1,6,1,6,2,1,0,6,5,1,3,0,1,0,7,5,1,6,6,3,2,3,1,6,4
4,2,0,6,0,0,4,6,3,3,3,6,7,0,1,4,5,7,3,4,2,3,7,7,1,4,6,6,4,5
0,2,6,1,6,0,3,3,6,1,4,4,3,5,0,7,3,7,3,7,0,2,7,1,7,2,5,7,1,0
7,4,5,2,2,2,0,3,1,4,0,6,0,3,7,1,4,6,6,6,6,1,1,6,1,3,3,3,2,0
2,0,1,3,1,7,2,0,3,0,5,2,3,4,1,7,1,0,1,5,3,6,1,2,2,0,5,2,4,0
7,3,2,6,2,6,6,1,0,1,0,6,2,3,0,5,2,1,7,0,0,3,3,0,1,1,1,4,6,1
4,1,7,4,2,5,7,1,4,0,4,3,6,1,6,6,1,0,4,0,0,0,2,2,4,2,6,0,7,5
1,1,2,1,0,5,1,6,2,6,0,4,4,6,2,5,1,1,0,0,5,0,1,1,2,5,1,3,6,6
0,3,7,3,1,4,6,1,2,7,1,0,4,4,1,1,6,7,1,5,0,4,0,4,6,3,1,4,1,0
1,4,7,0,4,1,5,3,1,0,0,6,0,0,5,0,3,5,4,7,2,1,4,7,3,1,5,7,3,2
3,1,5,3,3,4,2,4,4,5,1,7,6,0,3,3,0,0,7,7,3,5,6,7,0,2,1,0,2,2
3,1,4,4,1,5,1,5,6,2,6,4,5,7,0,0,4,1,3,7,5,1,0,4,5,4,5,1,1,0"""

lst = text.split('\n')

dct = dict()
for i, line in enumerate(lst):
    dct[i] = line.split(',')

print(dct[0])
#['0', '1', '5', '1', '3', '1', '0', '3', '1', '1', '0', '4', '7', '1', '0', '4', '0', '4', '1', '6', '5', '1', '1', '3', '6', '3', '4', '1', '5', '2']

或一行:

dct = {i: l.split(',') for i, l in enumerate(text.split('\n'))}

如果我没理解错的话,您是在尝试将文件的行读入数组。

您可以使用 str.split() 来做到这一点。

你的情况:

for line in f.readlines():
    a.append([int(i) for i in line.split(',')])

您可以使用字典并将 node/index 存储为键,将行存储为值:

file = open("file.txt")
raw_f = file.readlines()
d = {}
for line in raw_f:
    d|= {raw_f.index(line):line}

    
print(d)

例如: print(d[0]) 会给你第一行作为输出:

 0,1,5,1,3,1,0,3,1,1,0,4,7,1,0,4,0,4,1,6,5,1,1,3,6,3,4,1,5,2

如果您想留在列表中,请使用:

file = open("KontoN.txt")
raw_f = file.readlines()
res = []

for line in raw_f:
    res.append(line)

    
print(res)

然后:

print(res[0])

会给你:

0,1,5,1,3,1,0,3,1,1,0,4,7,1,0,4,0,4,1,6,5,1,1,3,6,3,4,1,5,2

删除数字之间的 , 可以使用替换,例如:

d|= {raw_f.index(line):line.replace(",","")}

或第二种情况:

res.append(line.replace(",",""))

对于 d[0]res[0] 两种情况下的输出都是:

015131031104710404165113634152

我推荐使用字典,因为查找时间比列表查找时间快。