0

I want to create edges using Networkx.I have a list of tuples that I extracted from my edge collection in arangodb.I want each two tuples become one tuple since each two shows an edge from one node of the graph to the other one. I do not know how to do this. I would appreciate it if you could help me with it. my list :

[('_from', 'account/10000022'), ('_to', 'customer/10000006'), ('_from', 'account/10000001'), ('_to', 'customer/10000008'), ('_from', 'account/10000034'), ('_to', 'customer/10000012'), ('_from', 'account/10000032'), ('_to', 'customer/10000011'), ('_from', 'account/10000027') ,('_to', 'customer/10000002')]

I want each two to become like this : [('_from', 'account/10000022'), ('_to', 'customer/10000006')]

  • Please read [ask] and https://meta.stackoverflow.com/questions/284236. It is not possible to "help you with it" because we do not have any understanding of *why* you "do not know how to do this". You say that you want to slice the list, and you show a desired result that looks like a slice of the list. And apparently you know the term "slicing" and understand what it means. So - *what is the problem*? Do you know the syntax to slice a list? Can you figure out which part of the list you want to slice? Then why not just do it? – Karl Knechtel Feb 20 '22 at 16:18
  • "I want each two tuples become one tuple" Okay, so you want to make many slices - is that the problem? Well - do you know how to repeat code? Can you think of a mathematical rule that tells you the end points of each slice? If you put those two things together, does it solve the problem? – Karl Knechtel Feb 20 '22 at 16:20
  • Does https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks answer your question? Please also read https://meta.stackoverflow.com/questions/261592. – Karl Knechtel Feb 20 '22 at 16:21

1 Answers1

2

Using list comprehension:

data = [('_from', 'account/10000022'), ('_to', 'customer/10000006'), ('_from', 'account/10000001'), ('_to', 'customer/10000008'), ('_from', 'account/10000034'), ('_to', 'customer/10000012'), ('_from', 'account/10000032'), ('_to', 'customer/10000011'), ('_from', 'account/10000027') ,('_to', 'customer/10000002')]

data = [(data[x], data[x+1]) for x in range(0, len(data), 2)]
print(data)
Cubix48
  • 2,607
  • 2
  • 5
  • 17