This question already has answers here:
Splitting a Python list into a list of overlapping chunks
(3 answers)
How can I iterate over overlapping (current, next) pairs of values from a list?
(12 answers)
Closed 3 months ago.
I have a list of integers like this
numbers = [1, 5, 7, 19, 22, 55]
I want to have a function that takes this as input and gives me a list of paired tuples that should contain the numbers as (1,5), (5,7), (7,19) and so on.
Kindly suggest.
I have tried using for loops. Didn't get expected output.
From Python 3.10 you can use itertools.pairwise
from itertools import pairwise
numbers = [1, 5, 7, 19, 22, 55]
list(pairwise(numbers)) # [(1, 5), (5, 7), (7, 19), (19, 22), (22, 55)]
lst = [(numbers[i],numbers[i+1]) for i in range(0,len(numbers)-1)]
This should do the trick: loop over all elements in the list numbers. You loop until the one to last element, since otherwise you would walk out of the array (get an index error).
Related
This question already has answers here:
Transpose list of lists
(14 answers)
Closed 9 months ago.
Input is [[159,159],[21,21]]
and I need
output like [[159,21],[159,21]]
I have searched python solution and tried a lot still am not getting solution, please share me solution.
Steps
zip the data together
convert the zipped tuple to list using map
convert the whole thing to be a list
Code
data = [[159,159],[21,21]]
print(list(map(list, zip(data[0],data[1]))))
Output
[[159,21],[159,21]]
You can use zip:
in_lists = [[159,159], [21,21]]
out_lists = list(zip(*in_lists))
print(out_lists)
which outputs
[(159, 21), (159, 21)]
The * in zip(*in_lists) will "unpack" the iterator in_lists and feed them to zip as separate arguments, which will be like zip([159, 159], [21, 21]). The zip function then combines the i-th element from all iterables into tuples, which results in [(159, 21), (159, 21)].
zip will do just that:
lst = [[159, 159], [21, 21]]
lst2 = list(zip(*lst))
print(lst2) # [(159, 21), (159, 21)]
if you need the nested elements to be lists as well:
lst2 = [list(item) for item in zip(*lst)] # [[159, 21], [159, 21]]
My apologies if this has been asked before, but I'm trying to implement the itertools permutations and combinations tools, but I can't get the packages to give the precise output I'm looking for. For instance, with the input:
[2,3,5,7,11,13,17,19]
I wish to output all possible ways of 'splitting' the list into tuples (not necessarily keeping the order of the given list):
[(2,3)(5)(7)(11)(13)(17)(19), (2,5)(3)(7)(11)(13)(17)(19), ..., (2,3,7)(5,11,17)(13)(19), ..., (2,3,5,7,11)(13,17,19)]
In other words, you can have more than one combination per element in the new list.
However I've got the following code (not claiming authorship to this line, though) to output all the isolated tuples:
[a for l in range(2,5) for a in itertools.combinations([2,3,5,7,11,13,17,19], l)].
i.e.:
[(2, 3), (2, 5), (2, 7), (2, 11), ..., (7, 13, 17, 19), (11, 13, 17, 19)]
Is there a function that can be used to give the desired output? (i.e. all possible groupings within all elements
Thank you!
This question already has answers here:
How to sort a list/tuple of lists/tuples by the element at a given index?
(11 answers)
Closed 2 years ago.
How can I sort a list in order? I have a list of tuples in a list and I need to sort them in order which I want.
list1 = [(2,3,4),(3,4,5),(5,3,2)]
I need to sort them firstly by the second element of tuple, secondly(if the previous were equal) by the third and thirdly by the first. Is there any built-in function to do it?
You can use sorted(iterable,key).
>>>sorted(list1,key=lambda x: (x[1],x[2],x[0])
#[(5, 3, 2), (2, 3, 4), (3, 4, 5)]
If you don't want to use lambda you can use itemgetter.
from operator import itemgetter
sorted(list1,key=itemgetter(1,2,0))
#[(5, 3, 2), (2, 3, 4), (3, 4, 5)]
This question already has answers here:
Remove partially duplicate tuples from list of tuples
(3 answers)
Making a sequence of tuples unique by a specific element
(1 answer)
Closed 3 years ago.
I have a list of tuples like this one here:
test = [('ent1', 24), ('ent2',12), ('ent3',4.5), ('ent1', 4), ('ent2', 3.5)]
I would like to remove those tuples from the list where the first element has already appeared. So the desired output would be
[('ent1', 24), ('ent2',12), ('ent3',4.5)]
I have no idea how to do this. Normally, if I would like to remove exact duplicated tuples, I would use
list(set(test))
but this is not working in this case. Has anybody an appropriate approach for this problem?
How do you like the output of dict(test)?
{'ent1': 4, 'ent2': 3.5, 'ent3': 4.5}
Or you may want to convert this back to a list of tuples with
>>> list(dict(test).items())
[('ent1', 4), ('ent2', 3.5), ('ent3', 4.5)]
Edit: This will keep the last assigned value but you can also keep the first assigned value by reversing first your list:
>>> list(dict(reversed(test)).items())
[('ent2', 12), ('ent1', 24), ('ent3', 4.5)]
Edit2: If you want to preserve list order, as well, this seems to be a good one-liner solution (inspired by Julien's answer):
>>> [(uk,next(v for k,v in test if k == uk)) for uk in dict(test).keys()]
[('ent1', 24), ('ent2', 12), ('ent3', 4.5)]
And finally, you with functools.reduce you can get another one-liner:
>>> from functools import reduce
>>> reduce(lambda lu,i:i[0] in dict(lu).keys() and lu or lu+[i], test, [])
[('ent1', 24), ('ent2', 12), ('ent3', 4.5)]
Explanation: lu is the list with only unique keys, i is the next item from the test list. If i[0], i.e. the key of the next element is in lu already, we keep lu, otherwise we append i.
Using a check flag
Ex:
test = [('ent1', 24), ('ent2',12), ('ent3',4.5), ('ent1', 4), ('ent2', 3.5)]
check_val = set() #Check Flag
res = []
for i in test:
if i[0] not in check_val:
res.append(i)
check_val.add(i[0])
print(res)
Output:
[('ent1', 24), ('ent2', 12), ('ent3', 4.5)]
test = [('ent1', 24), ('ent2',12), ('ent3',4.5), ('ent1', 4), ('ent2', 3.5)]
deduplicated_test = [(s,[t[1] for t in test if t[0] == s][0]) for s in sorted(set([t[0] for t in test]))]
Short and painful to read, sorry.
I don't remember why sorted(set()) works and set() doesn't but anyway...
This question already has answers here:
How to test if a list contains another list as a contiguous subsequence?
(19 answers)
Closed 9 years ago.
I have 2 lists one :
[(12,23),(12,45),(12,23),(2,5),(1,2),(2,4),(7,34)] which goes up to around 1000 elements
and another:
[(12,23),(12,45),(12,23),(2,5),(1,2),(2,66),(34,7)] which goes up to around 241 elements.
What I want is to check to see if the lists contain any of the same elements and then put them in a new list.
so the new list becomes
[(12,23),(12,45),(12,23),(2,5),(1,2)]
This doesn't include duplicates
>>> A = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,4),(7,34)]
>>> B = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,66),(34,7)]
>>> set(B).intersection(A) # note: making the smaller list to a set is faster
set([(12, 45), (1, 2), (12, 23), (2, 5)])
Or
>>> A = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,4),(7,34)]
>>> B = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,66),(34,7)]
>>> filter(set(B).__contains__, A)
[(12, 23), (12, 45), (12, 23), (2, 5), (1, 2)]
This returns every item in B if it occured in A, which produces the result you give in the example, however the set is probably what you want.
Since I don't know exactly what you are using this for, I'll suggest one more solution which returns a list, containing the items that occur in both lists, the minimum amount of times they occurred in either list (unordered). This differs from the set solution above which only returns each item the number of times it occurred in the other and doesn't care how many times it occurred in the first. This uses Counter for the intersection of multisets.
>>> from collections import Counter
>>> A = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,4),(7,34)]
>>> B = [(12,23),(12,45),(12,23),(2,5),(1,2),(2,66),(34,7)]
>>> list((Counter(A) & Counter(B)).elements())
[(1, 2), (12, 45), (12, 23), (12, 23), (2, 5)]