I got sample input as a=[(1,2),(2,3),(1,1),(2,1)], and the expected ouput is a=[(1,2),(2,3),(1,1)].
Here, (2,1) is removed, since the same combinational pair (1,2) is already available. I tried below code to remove duplicate pairs
map(tuple, set(frozenset(x) for x in a))
However, the output is [(1, 2), (2, 3), (1,)]. How to get (1,1) pair as (1,1) instead of (1,).
You can use a dict instead of a set to map the frozensets to the original tuple values. Build the dict in reversed order of the list so that duplicating tuples closer to the front can take precedence:
{frozenset(x): x for x in reversed(a)}.values()
This returns:
[(1, 2), (2, 3), (1, 1)]
This is one approach using sorted
Ex:
a=[(1,2),(2,3),(1,1),(2,1)]
print set([tuple(sorted(i)) for i in a])
Output:
set([(1, 2), (2, 3), (1, 1)])
Related
This is the code which I have did
from itertools import product
lst1=list(map(int,input().split()))
lst2=list(map(int,input().split()))
l3=product(lst1,lst2)
Input:
1 2
2 3
Output:
<itertools.product object at 0x7f02bdedb500>
Output that I want:
(1, 3) (1, 4) (2, 3) (2, 4)
I have tried adding parentheses, brackets and also tried to store the value in a variable and printed it. I still couldn't able to get that output. I don't want the output as a list, the expected output is shown above.
product(lst1,lst2) returns a itertools.product object just use map function to update internal tuple 1st index item or iterate though each element.
So use map function and update each tuple by 1 using lambda function :
l3= list(map(lambda i: (i[0], i[-1]+1), product(lst1,lst2)))
print(l3)
OUTPUT :
[(1, 3), (1, 4), (2, 3), (2, 4)]
Problem with this output is that it is string representation of list object so the best way is to go with this method.
So use iterate though this iterable object :
for i in l3:
i = list(i)
i[-1] += 1
print(tuple(i), end=' ')
OUTPUT :
(1, 3) (1, 4) (2, 3) (2, 4)
Convert or cast it to list, it works.
from itertools import product
lst1=list(map(int,input().split()))
lst2=list(map(int,input().split()))
l3=list(product(lst1,lst2))
"itertools.product" returns a generator, to get the list output you need :
list(l3)
However, not sure where you got your values from, I got :
[(1, 2), (1, 3), (2, 2), (2, 3)]
You can iterate l3 to get the desired output:
for i in l3:
print(i, end=" ")
Using Python 2.7, I have a list, and each element in the list is a (sub-list) -- a list of tuples. I want to remove the duplicate sub-list, I post input and expected output. I tried to use set, but there is error said list is not hashable. Anyone have good elegant code solutions? Thanks.
a=[[(1,2),(1,3)],
[(1,3),(1,2)],
[(1,2),(1,3)]]
# expect output
'''
a=[[(1,2),(1,3)],
[(1,3),(1,2)]]
'''
Try this! This is the simplest method.
a = [[(1,2),(1,3)],
[(1,3),(1,2)],
[(1,2),(1,3)]]
print [list(i) for i in set(map(tuple, a))]
[[(1, 3), (1, 2)], [(1, 2), (1, 3)]] //output
The above one is without any library support and also the ordering of the original list can't be ensured.
If you want to preserve the order of the list, then go for collections library.
from collections import OrderedDict
a = [[(1,2),(1,3)],
[(1,3),(1,2)],
[(1,2),(1,3)]]
print map(list, OrderedDict.fromkeys(map(tuple, a)))
[[(1, 2), (1, 3)], [(1, 3), (1, 2)]] //output
I think you can use a set, something like below
a=[[(1,2),(1,3)],
[(1,3),(1,2)],
[(1,2),(1,3)]]
unique_list = [list(x) for x in set(tuple(x) for x in a)]
print unique_list
Output:
[[(1, 3), (1, 2)], [(1, 2), (1, 3)]]
See working repl here - https://repl.it/EinE/1
Given a list l and all combinations of the list elements is it possible to remove any combination containing x while iterating over all combinations, so that you never consider a combination containing x during the iteration after it is removed?
for a, b in itertools.combinations(l, 2):
if some_function(a,b):
remove_any_tup_with_a_or_b(a, b)
My list l is pretty big so I don't want to keep the combinations in memory.
A cheap trick to accomplish this would be to filter by disjoint testing using a dynamically updated set of exclusion values, but it wouldn't actually avoid generating the combinations you wish to exclude, so it's not a major performance benefit (though filtering using a C built-in function like isdisjoint will be faster than Python level if checks with continue statements typically, by pushing the filter work to the C layer):
from future_builtins import filter # Only on Py2, for generator based filter
import itertools
blacklist = set()
for a, b in filter(blacklist.isdisjoint, itertools.combinations(l, 2)):
if some_function(a,b):
blacklist.update((a, b))
If you want to remove all tuples containing the number x from the list of combinations itertools.combinations(l, 2), consider that you there is a one-to-one mapping (mathematically speaking) from the set itertools.combinations([i for i in range(1,len(l)], 2) to the itertools.combinations(l, 2) that don't contain the number x.
Example:
The set of all of combinations from itertools.combinations([1,2,3,4], 2) that don't contain the number 1 is given by [(2, 3), (2, 4), (3, 4)]. Notice that the number of elements in this list is equal to the number of elements of combinations in the list itertools.combinations([1,2,3], 2)=[(1, 2), (1, 3), (2, 3)].
Since order doesn't matter in combinations, you can map 1 to 4 in [(1, 2), (1, 3), (2, 3)] to get [(1, 2), (1, 3), (2, 3)]=[(4, 2), (4, 3), (2, 3)]=[(2, 4), (3, 4), (2, 3)]=[(2, 3), (2, 4), (3, 4)].
Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple.
e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
Hint: use a custom key= function to extract the last element form each tuple.
The solution to the problem is:
def last(a):
return a[-1]
def sort_last(tuples):
return sorted(tuples, key=last)
Can anyone help me to understand what arguments are passed to the last function? Specifically, what does a contain?
We have not passed any values or arguments while calling the last function in the sorted method.
This is what is called a "lambda".
It's passing the current element of your list to the function "last" which will then get the last element of the tuple.
So the parameter "a" is the tuple being currently processed.
Iterating through a tuple via for loop - (Sorted initial list with 2nd element of the tuple.)
Nested for loops - comparing original tuple with the sorted 2nd element list.
tuple1 = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
List2 =[]
List3 =[]
for t in tuple1:
List2.append(t[1],)
List2.sort()
print(List2)
for l in List2:
for q in tuple1:
if l == int(q[1],):
List3.append(q)
print(List3)
I am before my last milestone preparing suitable output. Its quite simple, I think, but my approach doesn't work:
Given an dictionary like
mydict = {x1: (val1, dist1, (lat1, lon1)), x2: (val2, dist2, (lat2, lon2)), ...}
I tried to sort this in a nested way in an array, first using the "values" and if equals, sorting for "dist".
However, I did my homework and tried it using this way:
import operator
s = sorted(mydict.iteritems(), key=operator.itemgetter(1))
The thing is that if the sort method would be appliable to tuples, it would be quite easy, because it's already in the right order.
Unfortunately I get:
'list' object is not callable
Do you have an idea?
Is it possible you overwrote the name sorted with a list value?
The thing is that if the sort method would be appliable to tuples, it would be quite easy
sorted() works perfectly fine on a list of tuples, in which case it uses a lexicographic ordering.
(a,b) ≤ (a′,b′) if and only if a < a′ or (a = a′ and b ≤ b′).
This can be expanded to any number of dimensions.
>>> sorted([(10, 1), (5, 3), (6, 5), (3, 2), (5, 10)])
[(3, 2), (5, 3), (5, 10), (6, 5), (10, 1)]