opia
opia

Reputation: 65

Adjacency list creation in Python

I'm trying to implement an adjacency list by using the text file which includes:

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

Think of every line has the index number as the name instead of A,B,C.... So for example 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 is the node 0 and 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 is the node 1 etc. The problem here is I cannot extract elements because of the "," between each element. I tried this:

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

But it cannot go further, when it sees "," it stops.

Upvotes: 2

Views: 189

Answers (3)

Aru
Aru

Reputation: 352

You could use a dictionary and store the node/index as key and the line as a value:

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

    
print(d)

for example: print(d[0]) will give you the first line as output:

 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

if you want to stay with a list use:

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

for line in raw_f:
    res.append(line)

    
print(res)

then:

print(res[0])

will give you:

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

for removing the , between the numbers you can use replace for example:

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

or for 2nd case:

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

for the output in both cases d[0] and res[0] to be:

015131031104710404165113634152

I recommend using dictionaries, because the lookup time is faster than the lookup time in lists.

Upvotes: 2

Adin Ackerman
Adin Ackerman

Reputation: 312

If I understand correctly, you are trying to read lines of a file into an array.

You can do this with str.split().

In your case:

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

Upvotes: 1

Andreas
Andreas

Reputation: 9207

You can use split:

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']

Or in one line:

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

Upvotes: 2

Related Questions