How to write this in an eaiser way? - python

How could I write this in an eaiser way?
product_pairs =
[[(1, 2), (1, 3), (1, 6), (1, 8), (1, 9), (2, 3), (2, 6), (2, 8), (2, 9), (3, 6), (3,
8), (3, 9), (6, 8), (6, 9), (8, 9)],
[(0, 5), (0, 9), (5, 9)],
[(0, 9)],
[(0, 4), (0, 5), (0, 7), (0, 9), (4, 5), (4, 7), (4, 9), (5, 7), (5, 9), (7, 9)],
[(3, 8)],
[(1, 3), (1, 6), (1, 8), (3, 6), (3, 8), (6, 8)],
[(0, 5), (0, 9), (5, 9)],
[(3, 8)],
[(0, 4), (0, 5), (0, 7), (4, 5), (4, 7), (5, 7)],
[(0, 1), (0, 2), (0, 3), (0, 6), (1, 2), (1, 3), (1, 6), (2, 3), (2, 6), (3, 6)]]
cooccurences={}
d={}
pairs=[]
for lists in product_pairs:
for pair in lists:
if pair not in pairs: # If the pair is not in the dictionary
pairs.append(pair) # Storing all the pairs in a list
d[pair]=1 # Adding to the dictionary with value 1
else: # If the pair is already in the dictionary
d[pair]+=1 # Incrementing its value by 1
pairs.sort() # Sorting the pairs list
for pair in pairs: # Iterating through all pairs in sorted order
cooccurences[pair]=d[pair] # Storing their value in a dictionary
print(cooccurences) # Displaying output
Output:
{(0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 4): 2, (0, 5): 4, (0, 6): 1, (0, 7): 2, (0, 9): 4, (1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1, (2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1, (3, 6): 3, (3, 8): 4, (3, 9): 1, (4, 5): 2, (4, 7): 2, (4, 9): 1, (5, 7): 2, (5, 9): 3, (6, 8): 2, (6, 9): 1, (7, 9): 1, (8, 9): 1}

Best done using collections.Counter (also take a look at collections.defaultdict for a slightly more "manual" approach):
from collections import Counter
counter = Counter(pair for product_list in product_pairs for pair in product_list)
print(dict(counter.most_common()))

Related

How to sort this to get the expected sequence? [duplicate]

This question already has answers here:
How do I sort a dictionary by key?
(32 answers)
Closed 10 months ago.
How can i sort this to get the expected sequence?
My program:
product_pairs = [[(1, 2), (1, 3), (1, 6), (1, 8), (1, 9), (2, 3), (2, 6), (2, 8), (2, 9), (3, 6), (3, 8), (3, 9), (6, 8), (6, 9), (8, 9)], [(0, 5), (0, 9), (5, 9)], [(0, 9)], [(0, 4), (0, 5), (0, 7), (0, 9), (4, 5), (4, 7), (4, 9), (5, 7), (5, 9), (7, 9)], [(3, 8)], [(1, 3), (1, 6), (1, 8), (3, 6), (3, 8), (6, 8)], [(0, 5), (0, 9), (5, 9)], [(3, 8)], [(0, 4), (0, 5), (0, 7), (4, 5), (4, 7), (5, 7)], [(0, 1), (0, 2), (0, 3), (0, 6), (1, 2), (1, 3), (1, 6), (2, 3), (2, 6), (3, 6)]]
emptydic={}
cooccurences={}
for i in product_pairs:
for y in i:
if y not in emptydic:
emptydic[y]=1
else:
emptydic[y]+=1
print(emptydic)
my result:
{(1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1, (2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1, (3, 6): 3, (3, 8): 4, (3, 9): 1, (6, 8): 2, (6, 9): 1, (8, 9): 1, (0, 5): 4, (0, 9): 4, (5, 9): 3, (0, 4): 2, (0, 7): 2, (4, 5): 2, (4, 7): 2, (4, 9): 1, (5, 7): 2, (7, 9): 1, (0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 6): 1}
expected sequence:
{(0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 4): 2, (0, 5): 4, (0, 6): 1, (0, 7): 2, (0, 9): 4,
(1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1,
(2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1,
(3, 6): 3, (3, 8): 4, (3, 9): 1,
(4, 5): 2, (4, 7): 2, (4, 9): 1,
(5, 7): 2, (5, 9): 3,
(6, 8): 2, (6, 9): 1,
(7, 9): 1,
(8, 9): 1}
Thank you!
For Python 3.7 or higher:
dict(sorted(emptydic.items()))
{(0, 1): 1, (0, 2): 1, (0, 3): 1, (0, 4): 2, (0, 5): 4, (0, 6): 1, (0, 7): 2, (0, 9): 4, (1, 2): 2, (1, 3): 3, (1, 6): 3, (1, 8): 2, (1, 9): 1, (2, 3): 2, (2, 6): 2, (2, 8): 1, (2, 9): 1, (3, 6): 3, (3, 8): 4, (3, 9): 1, (4, 5): 2, (4, 7): 2, (4, 9): 1, (5, 7): 2, (5, 9): 3, (6, 8): 2, (6, 9): 1, (7, 9): 1, (8, 9): 1}

Min and Max value in list of tuples

I have a list of tuples for which I'm trying to get the min & max values at each index. Index 1 seems to be working correctly. For index 2, however, I'm expecting the value (-2,11), but I get (5, 5) instead. Can anyone explain what's going wrong please? Thank you.
test_list = [(-2, 5), (-1, 5), (0, 6), (0, 7), (0, 7), (1, 8), (2, 9), (2, 9), (3, 10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
res1 = max(test_list)[0],min(test_list)[0]
res2 = max(test_list)[1],min(test_list)[1]
print ("The min of index 1 : " + str(res1))
print ("The min of index 2 : " + str(res2))
#The min of index 1 : (10, -2)
#The min of index 2 : (5, 5)
Just try these list comprehensions:
first_elements = [elem[0] for elem in test_list]
second_elements = [elem[1] for elem in test_list]
print(min(first_elements), max(second_elements))
print(max(first_elements), min(second_elements))
Output:
"-2 11"
"10 -1"
To get the min and max of each element, use list comprehension like so:
test_list_min = [min(y[x] for y in test_list) for x in range(len(test_list[0]))]
test_list_max = [max(y[x] for y in test_list) for x in range(len(test_list[0]))]
print(test_list_min)
# [-2, -1]
print(test_list_max)
# [10, 11]
You can simple iterate over the list and get the tuples with a for loop.
for t in (test_list):
print("Max: ", max(t), "\tMin:", min(t))
Results:
Max: 5 Min: -2
Max: 5 Min: -1
Max: 6 Min: 0
Max: 7 Min: 0
Max: 7 Min: 0
Max: 8 Min: 1
...
You are reading an index of your max and min results with the line of code: max(test_list)[0],min(test_list)[0]
Try moving the index [] to the end of test_list like this: max(test_list[3]),min(test_list[3]).
You code should look like this:
test_list = [(-2, 5), (-1, 5), (0, 6), (0, 7), (0, 7), (1, 8), (2, 9), (2, 9), (3, 10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
max1, min1 = max(test_list[0]), min(test_list[0])
max2, min2 = max(test_list[1]), min(test_list[1])
print ("The min of index 1 : " + str(min1))
print ("The min of index 2 : " + str(min2))
print ("The max of index 1 : " + str(max1))
print ("The max of index 2 : " + str(max2))
Results:
The min of index 1 : -2
The min of index 2 : -1
The max of index 1 : 5
The max of index 2 : 5

Generating combinations of edges under a certain condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have edges of a graph represented as (node_from, node_to).
I want to efficiently generate all combinations of 2 edges of the form (0,x), where 0 is node 0 in my graph - in combination with all combinations of 2 edges of the form (x,n), where n is the "final node" (arbitrary i know) in my graph. I already have all the edges sitting around in a set, or also it is the case that every node contains an edge to every other node (so you could directly iterate through the range to generate combinations, for example).
A valid combination could be
(0,1),(0,5),(7,n),(11,n) or
(0,1),(0,5),(5,n),(11,n) or
(0,1),(0,n),(0,n),(11,n)
And, just to make it clear, I want combinations not permutations. I don't want to re-use the same group.
I am generally pretty good at figuring this stuff out, but I am having a bit of trouble with this one.
EDIT: updated to acommodate the requirements of "only two start/end edges"
I'm not sure what interface you have in mind, but from what I understood it looks like you can use filter() to select a subset of edges that "start in 0" or "end in n".
>>> edges = [(0,1), (0,5), (0,2), (5,3), (2,9), (4,6), (6,9), (3,9), (0,9)]
>>> edges_start = filter(lambda e: e[0] == 0, edges)
>>> edges_end = filter(lambda e: e[1] == 9, edges)
>>> edges_end
[(2, 9), (6, 9), (3, 9), (0, 9)]
>>> edges_start
[(0, 1), (0, 5), (0, 2), (0, 9)]
Now you can use itertools.combinations() to generate all possible pairs from each of the lists. Here's an example:
>>> import itertools
>>> list(itertools.combinations(edges_start, 2))
[((0, 1), (0, 5)), ((0, 1), (0, 2)), ((0, 1), (0, 9)), ((0, 5), (0, 2)), ((0, 5), (0, 9)), ((0, 2), (0, 9))]
Now you can plug in itertools.product() to generate all combinations of "pair from one list" and "pair from other list":
>>> edges_start_pairs = list(itertools.combinations(edges_start, 2))
>>> edges_end_pairs = list(itertools.combinations(edges_end, 2))
>>> pairs = list(itertools.product(edges_start_pairs, edges_end_pairs))
That's it! You can "flatten" the data structure if you like, but this is optional:
>>> flat_pairs = [list(p[0]+p[1]) for p in pairs]
Now let's pretty-print the results:
>>> from pprint import pprint
>>> pprint(flat_pairs)
[[(0, 1), (0, 5), (2, 9), (6, 9)],
[(0, 1), (0, 5), (2, 9), (3, 9)],
[(0, 1), (0, 5), (2, 9), (0, 9)],
[(0, 1), (0, 5), (6, 9), (3, 9)],
[(0, 1), (0, 5), (6, 9), (0, 9)],
[(0, 1), (0, 5), (3, 9), (0, 9)],
[(0, 1), (0, 2), (2, 9), (6, 9)],
[(0, 1), (0, 2), (2, 9), (3, 9)],
[(0, 1), (0, 2), (2, 9), (0, 9)],
[(0, 1), (0, 2), (6, 9), (3, 9)],
[(0, 1), (0, 2), (6, 9), (0, 9)],
[(0, 1), (0, 2), (3, 9), (0, 9)],
[(0, 1), (0, 9), (2, 9), (6, 9)],
[(0, 1), (0, 9), (2, 9), (3, 9)],
[(0, 1), (0, 9), (2, 9), (0, 9)],
[(0, 1), (0, 9), (6, 9), (3, 9)],
[(0, 1), (0, 9), (6, 9), (0, 9)],
[(0, 1), (0, 9), (3, 9), (0, 9)],
[(0, 5), (0, 2), (2, 9), (6, 9)],
[(0, 5), (0, 2), (2, 9), (3, 9)],
[(0, 5), (0, 2), (2, 9), (0, 9)],
[(0, 5), (0, 2), (6, 9), (3, 9)],
[(0, 5), (0, 2), (6, 9), (0, 9)],
[(0, 5), (0, 2), (3, 9), (0, 9)],
[(0, 5), (0, 9), (2, 9), (6, 9)],
[(0, 5), (0, 9), (2, 9), (3, 9)],
[(0, 5), (0, 9), (2, 9), (0, 9)],
[(0, 5), (0, 9), (6, 9), (3, 9)],
[(0, 5), (0, 9), (6, 9), (0, 9)],
[(0, 5), (0, 9), (3, 9), (0, 9)],
[(0, 2), (0, 9), (2, 9), (6, 9)],
[(0, 2), (0, 9), (2, 9), (3, 9)],
[(0, 2), (0, 9), (2, 9), (0, 9)],
[(0, 2), (0, 9), (6, 9), (3, 9)],
[(0, 2), (0, 9), (6, 9), (0, 9)],
[(0, 2), (0, 9), (3, 9), (0, 9)]]

Interaction between two lists

There are two lists:
A:[[0, 1, 3, 4, 6, 7], [2, 5]]
B:[(0, 5), (0, 9), (1, 7), (5, 0), (5, 9), (7, 1), (9, 0), (9, 5)]
The nummbers 0, 1, 2, 3, 4, 5, 6, 7 in A correspond to
(0, 5), (0, 9), (1, 7), (5, 0), (5, 9), (7, 1), (9, 0), (9, 5) in B
Is there a shortcut to obtain the following:
[[(0, 5), (0, 9), (5, 0), (5, 9), (9, 0), (9, 5)], [(1, 7),(7, 1)]]for A?
Use operator.itemgetter and list comprehension, like this
>>> indexes = [[0, 1, 3, 4, 6, 7], [2, 5]]
>>> data = [(0, 5), (0, 9), (1, 7), (5, 0), (5, 9), (7, 1), (9, 0), (9, 5)]
>>> from operator import itemgetter
>>> [list(itemgetter(*item)(data)) for item in indexes]
[[(0, 5), (0, 9), (5, 0), (5, 9), (9, 0), (9, 5)], [(1, 7), (7, 1)]]
Instead you can use nested list comprehension, like this
>>> [[data[index] for index in items] for items in indexes]
[[(0, 5), (0, 9), (5, 0), (5, 9), (9, 0), (9, 5)], [(1, 7), (7, 1)]]
You can simply use list comprehension:
>>> A=[[0, 1, 3, 4, 6, 7], [2, 5]]
>>> B=[(0, 5), (0, 9), (1, 7), (5, 0), (5, 9), (7, 1), (9, 0), (9, 5)]
>>> C=[[B[i] for i in l] for l in A]
>>> C
[[(0, 5), (0, 9), (5, 0), (5, 9), (9, 0), (9, 5)], [(1, 7), (7, 1)]]
Yes, the shortcut is:
[[B[j] for j in i] for i in A]

Use Python to create 2D coordinate

I am truly a novice in Python. Now, I am doing a project which involves creating a list of 2D coordinates. The coordinates should be uniformly placed, using a square grid (10*10), like(0,0)(0,1)(0,2)(0,3)...(0,10)(1,0)(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10).
Here is my code:
coordinate = []
x = 0
y = 0
while y < 10:
while x < 10:
coordinate.append((x,y))
x += 1
coordinate.append((x,y))
y += 1
print(coordinate)
But I can only get: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9)]
How can I rewrite my code to get all the points?
It's common to use a couple of for-loops to achieve this:
coordinates = []
for x in range(11):
for y in range(11):
coordinates.append((x, y))
It's also common to simplify this by flattening it into a list comprehension:
coordinates = [(x,y) for x in range(11) for y in range(11)]
from itertools import product
x = (0, 1, 2)
test = product(x, x)
Result:
>>> for ele in test:
... print ele
...
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
Note that test is a generator, so you probably would want to use list(test).
To actually answer your question, you forgot to reset x back to zero after the first run through x=0..9:
coordinate = []
y = 0
while y < 10:
x = 0
while x < 10:
coordinate.append((x,y))
x += 1
coordinate.append((x,y))
y += 1
print(coordinate)
Feel free to use all other variants, of course.
Use itertools.product:
>>> from itertools import product
>>> list(product(range(11), repeat=2))
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]
The above code is equivalent to this nested list comprehension:
>>> [(x, y) for x in range(11) for y in range(11)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]
Use a for loop. It lets you iterate over things called "iterators". range is a built-in function which returns an iterator from its starting argument (first argument) inclusive. up to its ending argument (second argument) non-inclusive. Thus range(0,11) will return 0,1,2,...,10.
coordinate = []
for y in range(0, 11):
for x in range(0, 11):
coordinate.append((x,y))
print(coordinate)
For more information on for loops in Python, check out the official wiki page.

Categories

Resources