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 6 years ago.
Improve this question
I have two lists say:
a = [1, 2, 2, 2, 3]
b = [2, 5, 6]
After doing a union, I should get something like this (don't mind the order):
c = [1, 2, 2, 2, 3, 5, 6]
The final list should contain common elements only once and rest of elements (from both lists) should be copied as they are. Sets can't be used for this as they remove multiple occurrence of an element from the list, which isn't a union. What is a Pythonic way to do so?
Perform a union, keeping repetition:
>>> c = a + b
[1, 2, 2, 3, 2, 5, 6]
Perform a union, keeping repetition & order:
>>> c = sorted(a + b)
[1, 2, 2, 2, 3, 5, 6]
Perform an union, no repetition in each list, but repetition allowed in final union, and keeped order:
>>> c = sorted(list(set(a)) + list(set(b)))
[1, 2, 2, 3, 5, 6]
After clarification of the question, the goal is to build a list that take elements (including repetition) of, and then add elements of b if they are not in the new list.
>>> c = a + [e for e in b if e not in a]
[1, 2, 2, 2, 3, 5, 6]
After another clarification of the question, the goal is to build a list containing all elements of input list. But, if elements are in common, they are pushed the same number there
>>> from collections import Counter
>>> def merge(a,b):
... na, nb = Counter(a), Counter(b)
... return list(Counter({k: max((na[k], nb[k])) for k in set(a + b)}).elements())
>>> merge([1, 2, 2, 2, 3], [2, 5, 6])
[1, 2, 2, 2, 3, 5, 6]
>>> merge([1, 2, 3], [2, 2, 4])
[1, 2, 2, 4]
Related
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 11 months ago.
Improve this question
I have a list in the form of [2, 1, 4, 3].
Is there a way to produce all of the possible sequences of the list values while keeping the same order? To further clarify, I am expecting something like this:
[1, 4, 3, 2],
[4, 3, 2, 1],
[3, 2, 1, 4],
[2, 1, 4, 3]
I would describe the problem as a case of "Reshuffling" but keeping the order constant, or imagining each of these sequences being circular, i.e. the final element in the list then becomes the first, etc. There are many ways to visualize this, hope it makes some sense.
>>> data = [4, 3, 2, 1]
[4, 3, 2, 1]
>>> l = len(data)
>>> for i in range(l):
... print(data[l - i:] + data[0:l - i])
...
[4, 3, 2, 1]
[1, 4, 3, 2]
[2, 1, 4, 3]
[3, 2, 1, 4]
Or if we want to capture them rather than print them:
[data[len(data)-i:] + data[0:len(data)-i] for i in range(len(data))]
Use this code:
l = [1, 4, 3, 2]
for element in l:
l.pop(0)
# removes the first element
l.append(element)
# appends the element at the end
print(l)
As I can see you want to remove the first element of the list and put it at the end.
You can use a deque to do something like this. Deque is a Doubly Eneded Queue and optimized for pop() and append() (in O(1) instead of O(n) for a list) and therefore well suited for a rotation that you are trying to do. Have a look at this blog post for more information.
from collections import deque
input_arr = [2, 1, 4, 3]
input_deque = deque(input_arr)
for i in range(len(input_arr)):
input_deque.rotate(-1)
print(list(input_deque))
Expected output:
[1, 4, 3, 2]
[4, 3, 2, 1]
[3, 2, 1, 4]
[2, 1, 4, 3]
Edit
As Chris commented, please keep the following in mind:
Be aware, rotating a deque is destructive. If you wanted to gather the rotated lists into a list, you'd want to be sure to put copies of the deque into that list, otherwise you'll end up with a list with four references to the same deque in the same state
— #Chris
To create a copy you can just call copy() on the created list which in my case would be done like this: list(input_deque).copy().
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
So I'm trying to find the items that went in and went out of a list before and after it changed
For example, at first my list is:
[1, 1, 2, 5, 7, 7]
And then a minute later it's:
[1, 2, 2, 5, 6, 7, 4]
How would I end up with 2 lists that show what items went out and which went in like so,
itemsOut = [1,7]
itemsIn = [2, 6, 4]
Position and length can change
You can use Counter from the built-in collections module:
>>> list1 = [1, 1, 2, 5, 7, 7]
>>> list2 = [1, 2, 2, 5, 6, 7, 4]
>>> from collections import Counter
>>> counter1 = Counter(list1)
>>> counter2 = Counter(list2)
>>> l_diff = counter1-counter2
>>> r_diff = counter2-counter1
>>> print(list(l_diff))
[1, 7]
>>> print(list(r_diff))
[2, 6, 4]
You could use something like this, to detect wich items changed.
old_arr = [1, 2, 2, 5, 7, 7]
new_arr = [1, 1, 2, 5, 7, 7]
items_out = []
for element in old_arr:
if new_arr.count(element) > 0:
new_arr.pop(new_arr.index(element))
else:
items_out.append(element)
print("Items out:", items_out)
print("Items in:", new_arr)
This question already has answers here:
Split List By Value and Keep Separators
(8 answers)
Closed 1 year ago.
Is there an easy way to split the list l below into 3 list. I want to cut the list when the sequence starts over. So every list should start with 1.
l= [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
l1 = [1, 2, 3,4, 5]
l2=[1,2,3,4]
l3=[1,2,3,4]
My original thought was to look at the lead value and implement a condition inside a for loop that would cut the list when x.lead < x. But how do I use lead and lag when using lists in python?
NumPy solution
import numpy as np
l = [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
parts = [list(i) for i in np.split(l,np.flatnonzero(np.diff(l)-1)+1)]
print(parts)
output
[[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4]]
Explanation: I first find differences between adjacent elements using numpy.diff, then subtract 1 to be able to use numpy.flatnonzero to find where difference is other than 1, add 1 (note that numpy.diff output length is input length minus 1) to get indices for use in numpy.split, eventually convert it to list, as otherwise you would end with numpy.arrays
What about this:
l = [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
one_indices = [i for i, e in enumerate(l) if e == 1]
slices = []
for count, item in enumerate(one_indices):
if count == len(one_indices) - 1:
slices.append((item, None))
else:
slices.append((item, one_indices[count + 1]))
sequences = [l[x[0] : x[1]] for x in slices]
print(sequences)
Out:
[[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4]]
Another way without numpy,
l= [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4]
start = 0
newlist = []
for i,v in enumerate(l):
if i!=0 and v==1:
newlist.append(l[start:i])
start = i
newlist.append(l[start:i+1])
print(newlist)
Working Demo: https://rextester.com/RYCV85570
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to replace variables in a list with combination of elements.
To be more specific:
I have these two lists
liste1 = [1,2,3,'X','X',4]
liste2 = [5,6,7]
and I want to get a list containing the elements below :
[1,2,3,5,6,4]
[1,2,3,5,7,4]
[1,2,3,6,7,4]
[1,2,3,6,5,4]
[1,2,3,7,5,4]
[1,2,3,7,6,4]
Does anyone have an idea how to make it ?
You can do it this way:
from itertools import permutations
liste1 = [1, 2, 3, 'X', 'X', 4]
liste2 = [5, 6, 7]
def replacements(liste1, liste2):
x_indices = [i for i, val in enumerate(liste1) if val == 'X']
nb = len(x_indices)
for perm in permutations(liste2, nb):
l1 = liste1[:] # if we want to preserve the original and yield different lists
for i, new_val in zip(x_indices, perm):
l1[i] = new_val
yield l1
for r in replacements(liste1, liste2):
print(r)
Output:
[1, 2, 3, 5, 6, 4]
[1, 2, 3, 5, 7, 4]
[1, 2, 3, 6, 5, 4]
[1, 2, 3, 6, 7, 4]
[1, 2, 3, 7, 5, 4]
[1, 2, 3, 7, 6, 4]
We first list the indices where 'X' appears, then generate the permutations of as many elements of liste2. For each permutation, we replace the 'X's.
This question already has answers here:
How to find most common elements of a list? [duplicate]
(11 answers)
Closed 6 years ago.
I have a list such as this:
lst = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
I would like to find all the elements which occur most frequently.
So I would like:
most = [1, 3, 5]
1, 3, and 5 would occur the most, which is 4 times. What's a fast, pythonic way to do this? I tried methods shown here:
How to find most common elements of a list?.
But it only gives me the top 3, I need all elements. Thank you.
With collections.Counter and a list comprehension:
from collections import Counter
lst = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
r = [x for x, _ in Counter(lst).most_common(3)]
print(r)
# [1, 3, 5]
You can generalize for values with highest count by using max on the counter values:
c = Counter(lst)
m = max(c.values())
r = [k for k in c if c[k] == m]
print(r)
# [1, 3, 5]
For large iterables, to efficiently iterate through the counter and stop once the required items have been taken, you can use itertools.takewhile with most_common without any parameters:
from itertools import takewhile
c = Counter(lst)
m = max(c.values())
r = [x for x, _ in takewhile(lambda x: x[1]==m, c.most_common())]
print(r)
# [1, 3, 5]
You gain by not having to iterate through all the items in the counter object, although there is some overhead with having to sort the items using most_common; so I'm sure if this absolutely efficient after all. You could do some experiments with timeit.
You can also get the same result with groupby from itertools module and list comprehension in this way:
from itertools import groupby
a = [1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
most_common = 3
final = [k for k,v in groupby(sorted(a), lambda x: x) if len(list(v)) > most_common]
Output:
print(final)
>>> [1, 3, 5]
You can do the following if you like to print all the most frequent,
from collections import Counter
words=[1, 3, 5, 1, 5, 6, 1, 1, 3, 4, 5, 2, 3, 4, 5, 3, 4]
most= [word for word, word_count in Counter(words).most_common()]
print (most)
>>>
[1, 3, 5, 4, 2, 6]
Please note, if you want to limit, you can enter the number inside most_common() function. Ex: ...most_common(3)]. Hope this answers your question.