I have a list,
selected=[[0,5,9,10,0],[0,2,4,7,8,0],[0,1,3,6,0]]
It's a route of 3 cars.I want to convert it below,
selected1=[(0,5),(5,9),(9,10),(10,0),(0,2),(2,4),(4,7),(7,8),(8,0),(0,1),(1,3),(3,6),(6,0)]
I can do reverse by using networkx library(selected1 to selected),but now i need selected to selected1.Thanks for everyone.
Using a list comprehension might do the work :
selected=[[0,5,9,10,0],[0,2,4,7,8,0],[0,1,3,6,0]]
selected1 = [(s[i], s[i+1]) for s in selected for i in range(len(s)-1)]
# [(0, 5), (5, 9), (9, 10), (10, 0), (0, 2), (2, 4), (4, 7), (7, 8), (8, 0), (0, 1), (1, 3), (3, 6), (6, 0)]
Related
I have a list B containing elements. I want to create all possible pairs using these elements as shown in the expected output. But I am getting an error. How do I fix it?
import numpy as np
import itertools
B=[ 1, 2, 5, 7, 10, 11]
combination=[]
for L in range(len(B) + 1):
for subset in itertools.combinations(B, L):
combination.append([list(sub) for sub in subset])
combination
The error is
in <listcomp>
combination.append([list(sub) for sub in subset])
TypeError: 'int' object is not iterable
The expected output is
[1,2],[1,5],[1,7],[1,10],[1,11],
[2,1],[2,5],[2,7],[2,10],[2,11],
[5,1],[5,2],[5,7],[5,10],[5,11],
[7,1],[7,2],[7,5],[7,10],[7,11],
[10,1],[10,2],[10,5],[10,7],[10,11],
[11,1],[11,2],[11,5],[11,7],[11,10]
What you are looking for is not combinations but permutations:
>>> list(itertools.permutations(B, 2))
[(1, 2),
(1, 5),
(1, 7),
(1, 10),
(1, 11),
(2, 1),
(2, 5),
(2, 7),
(2, 10),
(2, 11),
(5, 1),
(5, 2),
(5, 7),
(5, 10),
(5, 11),
(7, 1),
(7, 2),
(7, 5),
(7, 10),
(7, 11),
(10, 1),
(10, 2),
(10, 5),
(10, 7),
(10, 11),
(11, 1),
(11, 2),
(11, 5),
(11, 7),
(11, 10)]
To convert to a list:
# List comprehension
perm = [list(p) for p in itertools.permutations(B, 2)
# Functional programming
perm = list(map(list, itertools.permutations(B, 2)))
Beside the #Corralien answer which is the correct one, i am just correcting your solution to let you know that your solution can also be made to work just changing few things
Do not convert & iterate subset as it is already a list and its element is not iterable
Replace combinations with permutations
Add a condition to filter only two combinations as your code is producing permutation of N X N
Final version is following.
import numpy as np
import itertools
B=[ 1, 2, 5, 7, 10, 11]
combination=[]
for L in range(len(B) + 1):
for subset in itertools.permutations(B, L):
if len(subset) == 2:
combination.append(subset)
print(combination)
OUTPUT
[(1, 2), (1, 5), (1, 7), (1, 10), (1, 11), (2, 1), (2, 5), (2, 7), (2, 10), (2, 11), (5, 1), (5, 2), (5, 7), (5, 10), (5, 11), (7, 1), (7, 2), (7, 5), (7, 10), (7, 11), (10, 1), (10, 2), (10, 5), (10, 7), (10, 11), (11, 1), (11, 2), (11, 5), (11, 7), (11, 10)]
I have a directed graph G represented as a networkx.digraph. I want to be able to do shortest-path computations on the undirected version of that graph. How do I get an object that is the undirected version of that graph.
I know it will involve making a graph view, however
the documentation for generic_graph_view is not very useful in explaining how to achieve this; nor is the code itself for some one that is not familiar with the internals of the library.
You can just pass your directed graph to nx.Graph
G=nx.fast_gnp_random_graph(10,.2,directed=True)
G_undirected = nx.Graph(G)
print(G.edges)
# OutEdgeView([(0, 1), (0, 5), (1, 0), (1, 2), (1, 6), (1, 9),
# (2, 7), (2, 9), (3, 4), (4, 7), (5, 4), (6, 0), (7, 8),
# (8, 9), (9, 4)])
print(G_undirected.edges)
# EdgeView([(0, 1), (0, 5), (0, 6), (1, 2), (1, 6), (1, 9), (2, 7),
# (2, 9), (3, 4), (4, 7), (4, 5), (4, 9), (7, 8), (8, 9)])
I have a list:
points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5),
(3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),
(-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2),
(5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]
equal_values = 0
I'd like to count how many times in this list each pair of values are equal e.g. (2, 2). How can I define that condition in an if statement?
if ??? in points:
equal_values +=1
You can do it quickly by this:
len([i for i in points if i[0]==i[1]])
Or with a common for loop combined with if statement, like below:
equal_values = 0
for i in points:
if i[0]==i[1]:
equal_points+=1
The output is 3 for both ways, for your given list
You want this ?
points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5),
(3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),
(-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2),
(5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]
print(points.count((2, 2)))
To count the number of the (2, 2) occurence in your list ?
this should work:
equal = [i for i in points if i[0] == i[1]]
then get the count (length of the list):
len(equal)
how does that work?
it loops through the elements in points and checks if the 0th and the 1st index's are equal
it appends that item to a list if it is equal
we get the count of equal pairs by checking the length of the list
Try this:
for x in points:
m = list(iterable(x))
if len(set(m)) == 1:
equal_values += 1
This is my first question here =):
My problem is what is stated in the title. I want to simply sort a list of tuples with 2 elements. It should work with sorted but it still returns the unsorted List,... it seems to do nothing.
Input:
#"L =[(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5)]" Its an example.
for line in stdin:
L = [int(i) for i in line.split()]
n = L[0]
V = [i for i in range(n)]
edgelist = L[1:]
EDGE = [(edgelist[i],edgelist[i+1]) for i in range(0,len(edgelist)-1,2) ]
mK = missingKnots(edgelist)
EDGE = sorted(EDGE)
EDGE = list(set(EDGE))
Output:
[(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5)]
Should be:
[(0, 1), (0, 4), (0, 6), (2, 5), (4, 6), (5, 7)]
Unsorted =(.
Thanks for reading!
Sort and return new list:
>>> L =[(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5)]
>>> sorted(L)
[(0, 1), (0, 4), (0, 6), (2, 5), (4, 6), (5, 7)]
Sort in place:
>>> L.sort()
>>> L
[(0, 1), (0, 4), (0, 6), (2, 5), (4, 6), (5, 7)]
Don't do the set() afterward. Sets are unordered.
>>> list(set(L))
[(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5)] # now it's messed up again.
Ideally, to sort and remove duplicates:
>>> L = [(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5), (0, 1)]
>>> sorted(set(L))
[(0, 1), (0, 4), (0, 6), (2, 5), (4, 6), (5, 7)]
With lambda expression, you can customise the rules of sorting.
EDGE = [(0, 1), (4, 6), (5, 7), (0, 6), (0, 4), (2, 5)]
EDGE = sorted(EDGE, key=lambda x: (x[0],x[1]) )
print EDGE
result:
[(0, 1), (0, 4), (0, 6), (2, 5), (4, 6), (5, 7)]
I'm trying generate a bunch of pairs from a list in python --- I figured out a way of doing it using for loops:
keys = range(10)
keypairs = list()
for i in range(len(keys)):
for j in range(i+1, len(keys)):
keypairs = keypairs + [(keys[i], keys[j])]
Is there a more "python style" way of doing this? My method doesn't seem very elegant ...
You want two range loops, one from 0 to n and the inner from each i of the first range + 1 to n using a list comp:
n = 10
pairs = [(i, j) for i in range(n) for j in range(i+1, n)]
from pprint import pprint as pp
pp(pairs,compact=True)
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 2),
(1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 3), (2, 4), (2, 5),
(2, 6), (2, 7), (2, 8), (2, 9), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9),
(4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 6), (5, 7), (5, 8), (5, 9), (6, 7),
(6, 8), (6, 9), (7, 8), (7, 9), (8, 9)]
Which exactly matches your output.
You want to check list comprehension:
sorted([(i, j) for j in range(10) for i in range(10) if j > i])
I would use itertools for that. Check combinations_with_replacement and combinations methods.
And here is the code sample:
import itertools
list(itertools.combinations(keys, 2))
EDITED: Yes, I noticed that it should be combinations, not combinations_with_replacements.