My question is pretty straight forward:-
in python itertools why can I get a print of all permutations for say [,r =3]:
>>>import itertools
>>>print list(itertools.permutations([1,2,3], 3))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
However only an empty list for [,r >3 e.g 10]:
>>>print list(itertools.permutations([1,2,3], 10))
[]
is there away of getting round this??
As stated in the itertools docs permutations() does not repeat elements.
I think you want combinations_with_replacement()
>>> from itertools import combinations_with_replacement
>>> print list(combinations_with_replacement([1, 2, 3], 10))
[(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 1, 1, 1, 1, 1, 3, 3), (1, 1, 1, 1, 1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2, 2, 3), (1, 1, 1, 1, 1, 1, 1, 2, 3, 3), (1, 1, 1, 1, 1, 1, 1, 3, 3, 3), (1, 1, 1, 1, 1, 1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 2, 2, 2, 3), (1, 1, 1, 1, 1, 1, 2, 2, 3, 3), (1, 1, 1, 1, 1, 1, 2, 3, 3, 3), (1, 1, 1, 1, 1, 1, 3, 3, 3, 3), (1, 1, 1, 1, 1, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 2, 2, 2, 2, 3), (1, 1, 1, 1, 1, 2, 2, 2, 3, 3), (1, 1, 1, 1, 1, 2, 2, 3, 3, 3), (1, 1, 1, 1, 1, 2, 3, 3, 3, 3), (1, 1, 1, 1, 1, 3, 3, 3, 3, 3), (1, 1, 1, 1, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 2, 2, 2, 2, 2, 3), (1, 1, 1, 1, 2, 2, 2, 2, 3, 3), (1, 1, 1, 1, 2, 2, 2, 3, 3, 3), (1, 1, 1, 1, 2, 2, 3, 3, 3, 3), (1, 1, 1, 1, 2, 3, 3, 3, 3, 3), (1, 1, 1, 1, 3, 3, 3, 3, 3, 3), (1, 1, 1, 2, 2, 2, 2, 2, 2, 2), (1, 1, 1, 2, 2, 2, 2, 2, 2, 3), (1, 1, 1, 2, 2, 2, 2, 2, 3, 3), (1, 1, 1, 2, 2, 2, 2, 3, 3, 3), (1, 1, 1, 2, 2, 2, 3, 3, 3, 3), (1, 1, 1, 2, 2, 3, 3, 3, 3, 3), (1, 1, 1, 2, 3, 3, 3, 3, 3, 3), (1, 1, 1, 3, 3, 3, 3, 3, 3, 3), (1, 1, 2, 2, 2, 2, 2, 2, 2, 2), (1, 1, 2, 2, 2, 2, 2, 2, 2, 3), (1, 1, 2, 2, 2, 2, 2, 2, 3, 3), (1, 1, 2, 2, 2, 2, 2, 3, 3, 3), (1, 1, 2, 2, 2, 2, 3, 3, 3, 3), (1, 1, 2, 2, 2, 3, 3, 3, 3, 3), (1, 1, 2, 2, 3, 3, 3, 3, 3, 3), (1, 1, 2, 3, 3, 3, 3, 3, 3, 3), (1, 1, 3, 3, 3, 3, 3, 3, 3, 3), (1, 2, 2, 2, 2, 2, 2, 2, 2, 2), (1, 2, 2, 2, 2, 2, 2, 2, 2, 3), (1, 2, 2, 2, 2, 2, 2, 2, 3, 3), (1, 2, 2, 2, 2, 2, 2, 3, 3, 3), (1, 2, 2, 2, 2, 2, 3, 3, 3, 3), (1, 2, 2, 2, 2, 3, 3, 3, 3, 3), (1, 2, 2, 2, 3, 3, 3, 3, 3, 3), (1, 2, 2, 3, 3, 3, 3, 3, 3, 3), (1, 2, 3, 3, 3, 3, 3, 3, 3, 3), (1, 3, 3, 3, 3, 3, 3, 3, 3, 3), (2, 2, 2, 2, 2, 2, 2, 2, 2, 2), (2, 2, 2, 2, 2, 2, 2, 2, 2, 3), (2, 2, 2, 2, 2, 2, 2, 2, 3, 3), (2, 2, 2, 2, 2, 2, 2, 3, 3, 3), (2, 2, 2, 2, 2, 2, 3, 3, 3, 3), (2, 2, 2, 2, 2, 3, 3, 3, 3, 3), (2, 2, 2, 2, 3, 3, 3, 3, 3, 3), (2, 2, 2, 3, 3, 3, 3, 3, 3, 3), (2, 2, 3, 3, 3, 3, 3, 3, 3, 3), (2, 3, 3, 3, 3, 3, 3, 3, 3, 3), (3, 3, 3, 3, 3, 3, 3, 3, 3, 3)]
Related
I have a list of sequences of numbers in which I am trying to find the recurring patterns.
Here is what I did to find the recurring pattern in one sequence of numbers:
import collections
import more_itertools
sequence = [
1, 2, 4, 1, 4, 1, 2, 4, 6, 7
]
size_2 = 2
size_3 = 3
size_4 = 4
window_2 = [
tuple(window)
for window in more_itertools.windowed(sequence, size_2)
]
window_3 = [
tuple(window)
for window in more_itertools.windowed(sequence, size_3)
]
window_4 = [
tuple(window)
for window in more_itertools.windowed(sequence, size_4)
]
counter_2 = collections.Counter(window_2)
counter_3 = collections.Counter(window_3)
counter_4 = collections.Counter(window_4)
for window, count in counter_2.items():
if count > 1:
print(window, count)
for window, count in counter_3.items():
if count > 1:
print(window, count)
for window, count in counter_4.items():
if count > 1:
print(window, count)
I'm sure there is a much better way to write this code, but since I'm new to Python, this is what I could come up with. The output of the above code is:
(1, 2) 2
(2, 4) 2
(4, 1) 2
(1, 2, 4) 2
Now I need to do this for a series of sequences, for example:
sequence = [
[1, 2, 4, 1, 4, 1, 2, 4, 6, 7],
[3, 1, 3, 4, 3, 4, 2, 4, 7, 4, 7, 4, 6, 7, 6, 7],
[2, 4, 1, 2, 3, 2, 3, 4, 1, 3, 4, 2, 4, 6, 4, 1, 4, 6],
]
I need to find the recurring patterns in each sequence separately. An example output would be:
[1, 2, 4, 1, 4, 1, 2, 4, 6, 7]
(1, 2) 2
(2, 4) 2
(4, 1) 2
(1, 2, 4) 2
[3, 1, 3, 4, 3, 4, 2, 4, 7, 4, 7, 4, 6, 7, 6, 7]
(3, 4) 2
(4, 7) 2
(7, 4) 2
(6, 7) 2
(4, 7, 4) 2
etc.
I've tried different ways with no luck. Any help would be appreciated.
You can use multiple for-loop base sequence and size.
from collections import Counter
import more_itertools
sequence = [
[1, 2, 4, 1, 4, 1, 2, 4, 6, 7],
[3, 1, 3, 4, 3, 4, 2, 4, 7, 4, 7, 4, 6, 7, 6, 7],
[2, 4, 1, 2, 3, 2, 3, 4, 1, 3, 4, 2, 4, 6, 4, 1, 4, 6],
]
for seq in sequence:
print(seq)
for size in [2, 3, 4]:
for win, cnt in Counter(
window
for window in more_itertools.windowed(seq, size)
).items():
if cnt > 1:
print(win, cnt)
print()
[1, 2, 4, 1, 4, 1, 2, 4, 6, 7]
(1, 2) 2
(2, 4) 2
(4, 1) 2
(1, 2, 4) 2
[3, 1, 3, 4, 3, 4, 2, 4, 7, 4, 7, 4, 6, 7, 6, 7]
(3, 4) 2
(4, 7) 2
(7, 4) 2
(6, 7) 2
(4, 7, 4) 2
[2, 4, 1, 2, 3, 2, 3, 4, 1, 3, 4, 2, 4, 6, 4, 1, 4, 6]
(2, 4) 2
(4, 1) 3
(2, 3) 2
(3, 4) 2
(4, 6) 2
You can save result in dict.
dct = {str(seq) : [
(win, cnt)
for size in [2, 3, 4]
for win, cnt in Counter(window for window in more_itertools.windowed(seq, size)).items()
if cnt > 1]
for seq in sequence}
print(dct)
{'[1, 2, 4, 1, 4, 1, 2, 4, 6, 7]':
[((1, 2), 2),
((2, 4), 2),
((4, 1), 2),
((1, 2, 4), 2)],
'[3, 1, 3, 4, 3, 4, 2, 4, 7, 4, 7, 4, 6, 7, 6, 7]':
[((3, 4), 2),
((4, 7), 2),
((7, 4), 2),
((6, 7), 2),
((4, 7, 4), 2)],
'[2, 4, 1, 2, 3, 2, 3, 4, 1, 3, 4, 2, 4, 6, 4, 1, 4, 6]':
[((2, 4), 2),
((4, 1), 3),
((2, 3), 2),
((3, 4), 2),
((4, 6), 2)]}
From choices [1,2,3], I want to output all possible combinations, allowing duplicates in the choices, in a list of 5 elements.
In each of the lists, there must be at least one of 1, at least one of 2, and at least one of 3.
A clumsy way as below. It firstly generates a list of 5 using either in [1,2,3]. All generated lists are examined to have at least each of [1,2,3]. The qualified ones are put into a big list. Then the duplicates in the big list are removed (loop it many times to make sure good coverage):
import random
import itertools
choices = [1,2,3]
big_list = []
for a in range(10000):
new_list = [random.choice(choices) for i in range(5)]
if new_list.count(1) >= 1 and new_list.count(2) >= 1 and new_list.count(3) >= 1:
big_list.append(new_list)
big_list.sort()
final_list = list(big_list for big_list, _ in itertools.groupby(big_list))
# this line to remove the duplicates in the list of lists
print (final_list)
Considering the sequence matters, that is, [1,1,1,2,3] and [2,3,1,1,1] are two different lists.
What would be the smarter and more comprehensive way to do so?
Maybe you could could use itertools.combinations_with_replacement, itertools.permutations along with collections.Counter:
>>> from collections import Counter
>>> from itertools import combinations_with_replacement, permutations
>>>
>>> def is_valid_combination(comb: tuple) -> bool:
... digit_counts = Counter(comb)
... return digit_counts[1] >= 1 and \
... digit_counts[2] >= 1 and \
... digit_counts[3] >= 1
...
>>> choices = [1, 2, 3]
>>> valid_combinations = [
... c for c in combinations_with_replacement(choices, r=5)
... if is_valid_combination(c)
... ]
>>>
>>> valid_combinations
[(1, 1, 1, 2, 3), (1, 1, 2, 2, 3), (1, 1, 2, 3, 3), (1, 2, 2, 2, 3), (1, 2, 2, 3, 3), (1, 2, 3, 3, 3)]
>>>
>>> all_permutations_of_valid_combinations = {
... p
... for c in valid_combinations for p in permutations(c)
... }
>>>
>>> all_permutations_of_valid_combinations
{(2, 1, 3, 1, 2), (2, 1, 3, 2, 1), (3, 3, 2, 1, 3), (1, 2, 3, 2, 3), (1, 2, 1, 3, 1), (3, 1, 2, 3, 2), (3, 3, 3, 2, 1), (3, 2, 2, 1, 1), (1, 2, 2, 3, 1), (1, 3, 2, 2, 3), (1, 3, 2, 3, 2), (1, 2, 1, 1, 3), (3, 1, 3, 3, 2), (3, 1, 1, 2, 3), (2, 1, 3, 2, 3), (1, 2, 2, 1, 3), (1, 2, 1, 3, 3), (2, 3, 3, 1, 2), (2, 3, 3, 2, 1), (3, 3, 1, 2, 1), (3, 2, 3, 2, 1), (1, 2, 2, 3, 3), (3, 2, 1, 1, 1), (2, 2, 1, 3, 1), (2, 3, 1, 1, 1), (1, 3, 1, 2, 3), (3, 3, 1, 1, 2), (3, 2, 3, 1, 2), (2, 1, 2, 3, 1), (2, 2, 1, 1, 3), (3, 2, 1, 3, 1), (2, 3, 1, 3, 1), (1, 1, 3, 2, 1), (2, 3, 2, 1, 2), (2, 3, 2, 2, 1), (2, 1, 2, 1, 3), (3, 2, 1, 1, 3), (2, 2, 1, 3, 3), (2, 3, 1, 1, 3), (2, 3, 1, 2, 2), (3, 2, 3, 3, 1), (1, 1, 3, 1, 2), (2, 1, 2, 3, 3), (3, 3, 2, 2, 1), (3, 1, 2, 1, 2), (3, 2, 1, 3, 3), (3, 1, 2, 2, 1), (2, 3, 1, 3, 3), (1, 1, 3, 2, 3), (3, 3, 3, 1, 2), (1, 2, 3, 1, 1), (1, 1, 3, 3, 2), (3, 1, 3, 1, 2), (2, 3, 2, 3, 1), (1, 3, 2, 1, 1), (2, 1, 3, 3, 1), (3, 2, 2, 3, 1), (3, 1, 2, 2, 3), (1, 3, 2, 2, 2), (1, 2, 3, 1, 3), (1, 3, 2, 3, 1), (3, 2, 2, 1, 3), (2, 2, 3, 2, 1), (3, 1, 1, 2, 2), (1, 1, 2, 2, 3), (2, 1, 3, 2, 2), (1, 3, 3, 2, 2), (3, 3, 1, 3, 2), (2, 1, 1, 3, 1), (1, 3, 2, 1, 3), (2, 1, 3, 3, 3), (3, 1, 3, 2, 2), (2, 2, 3, 1, 2), (1, 1, 2, 3, 1), (3, 2, 1, 2, 2), (1, 2, 2, 3, 2), (3, 3, 1, 2, 3), (1, 3, 2, 3, 3), (1, 2, 1, 2, 3), (3, 2, 3, 1, 1), (1, 3, 1, 2, 2), (1, 2, 2, 2, 3), (2, 1, 1, 3, 3), (3, 1, 1, 3, 2), (1, 1, 2, 3, 3), (1, 3, 3, 3, 2), (2, 3, 2, 1, 1), (2, 2, 1, 2, 3), (2, 2, 1, 3, 2), (1, 2, 3, 3, 1), (3, 2, 3, 1, 3), (2, 3, 1, 2, 1), (2, 1, 3, 1, 1), (3, 3, 2, 1, 2), (1, 2, 3, 2, 2), (1, 3, 1, 3, 2), (3, 1, 2, 3, 1), (2, 2, 2, 3, 1), (2, 1, 2, 2, 3), (1, 2, 3, 3, 3), (2, 3, 1, 2, 3), (2, 1, 3, 1, 3), (3, 2, 2, 2, 1), (1, 2, 1, 3, 2), (2, 3, 3, 1, 1), (3, 1, 2, 3, 3), (3, 2, 2, 1, 2), (3, 1, 1, 2, 1), (1, 3, 3, 2, 1), (2, 3, 3, 3, 1), (2, 1, 1, 1, 3), (1, 3, 2, 1, 2), (2, 1, 3, 3, 2), (1, 1, 1, 2, 3), (3, 1, 3, 2, 1), (1, 1, 1, 3, 2), (2, 2, 3, 1, 1), (3, 1, 1, 1, 2), (1, 1, 2, 1, 3), (1, 3, 3, 1, 2), (3, 2, 1, 2, 1), (2, 3, 3, 1, 3), (3, 3, 1, 2, 2), (2, 2, 3, 3, 1), (1, 3, 1, 2, 1), (1, 3, 3, 2, 3), (3, 2, 1, 1, 2), (2, 1, 1, 3, 2), (2, 3, 1, 1, 2), (3, 1, 3, 2, 3), (2, 2, 3, 1, 3), (1, 3, 1, 1, 2), (1, 1, 2, 3, 2), (2, 1, 2, 3, 2), (3, 2, 1, 2, 3), (3, 1, 2, 1, 1), (3, 2, 1, 3, 2), (2, 1, 1, 2, 3), (2, 3, 1, 3, 2), (1, 1, 3, 2, 2), (2, 3, 2, 1, 3), (3, 3, 2, 3, 1), (3, 3, 2, 1, 1), (1, 2, 3, 2, 1), (3, 1, 2, 1, 3), (2, 2, 2, 1, 3), (3, 1, 2, 2, 2), (1, 3, 2, 2, 1), (1, 2, 3, 1, 2), (1, 2, 3, 3, 2)}
Apart from the itertools.combinations itself, you could use some recursive logic:
def combinations(choices, n = 5):
if n == 1:
return [[x,] for x in choices]
else:
return [v + [x,] for v in combinations(choices, n = n -1) for x in choices]
To select only the combinations which contain at least one 1, 2 and 3:
[x for x in combinations(choices, n = 5) if all(c in x for c in choices)]
Let's say I have 4 buckets and an array of numbers like [1,2,3,4,5]
| || || || |
|__||__||__||__|
1 2 3 4
5 2 3 4
1 5 3 4
etc...
I can also have less than 4 numbers like [1, 2, 3]
| || || || |
|__||__||__||__|
1 2 3
1 3 2
1 2 3
etc...
How do I find all possible combinations of the numbers in buckets (like [1,2,3,4] if length is >= 4 or [1,2,None,None] if length is < 4)?
You can use a recursive generator function:
def combos(nums, buckets, c = []):
if len(c) == buckets:
yield c
else:
if len(c) + len(nums) < buckets:
yield from combos(nums, buckets, c+[None])
for i, a in enumerate(nums):
yield from combos(nums[:i]+nums[i+1:], buckets, c+[a])
print(list(combos([1, 2, 3, 4, 5], 4)))
print(list(combos([1, 2, 3], 4)))
Output:
[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 3], [1, 2, 4, 5], [1, 2, 5, 3], [1, 2, 5, 4], [1, 3, 2, 4], [1, 3, 2, 5], [1, 3, 4, 2], [1, 3, 4, 5], [1, 3, 5, 2], [1, 3, 5, 4], [1, 4, 2, 3], [1, 4, 2, 5], [1, 4, 3, 2], [1, 4, 3, 5], [1, 4, 5, 2], [1, 4, 5, 3], [1, 5, 2, 3], [1, 5, 2, 4], [1, 5, 3, 2], [1, 5, 3, 4], [1, 5, 4, 2], [1, 5, 4, 3], [2, 1, 3, 4], [2, 1, 3, 5], [2, 1, 4, 3], [2, 1, 4, 5], [2, 1, 5, 3], [2, 1, 5, 4], [2, 3, 1, 4], [2, 3, 1, 5], [2, 3, 4, 1], [2, 3, 4, 5], [2, 3, 5, 1], [2, 3, 5, 4], [2, 4, 1, 3], [2, 4, 1, 5], [2, 4, 3, 1], [2, 4, 3, 5], [2, 4, 5, 1], [2, 4, 5, 3], [2, 5, 1, 3], [2, 5, 1, 4], [2, 5, 3, 1], [2, 5, 3, 4], [2, 5, 4, 1], [2, 5, 4, 3], [3, 1, 2, 4], [3, 1, 2, 5], [3, 1, 4, 2], [3, 1, 4, 5], [3, 1, 5, 2], [3, 1, 5, 4], [3, 2, 1, 4], [3, 2, 1, 5], [3, 2, 4, 1], [3, 2, 4, 5], [3, 2, 5, 1], [3, 2, 5, 4], [3, 4, 1, 2], [3, 4, 1, 5], [3, 4, 2, 1], [3, 4, 2, 5], [3, 4, 5, 1], [3, 4, 5, 2], [3, 5, 1, 2], [3, 5, 1, 4], [3, 5, 2, 1], [3, 5, 2, 4], [3, 5, 4, 1], [3, 5, 4, 2], [4, 1, 2, 3], [4, 1, 2, 5], [4, 1, 3, 2], [4, 1, 3, 5], [4, 1, 5, 2], [4, 1, 5, 3], [4, 2, 1, 3], [4, 2, 1, 5], [4, 2, 3, 1], [4, 2, 3, 5], [4, 2, 5, 1], [4, 2, 5, 3], [4, 3, 1, 2], [4, 3, 1, 5], [4, 3, 2, 1], [4, 3, 2, 5], [4, 3, 5, 1], [4, 3, 5, 2], [4, 5, 1, 2], [4, 5, 1, 3], [4, 5, 2, 1], [4, 5, 2, 3], [4, 5, 3, 1], [4, 5, 3, 2], [5, 1, 2, 3], [5, 1, 2, 4], [5, 1, 3, 2], [5, 1, 3, 4], [5, 1, 4, 2], [5, 1, 4, 3], [5, 2, 1, 3], [5, 2, 1, 4], [5, 2, 3, 1], [5, 2, 3, 4], [5, 2, 4, 1], [5, 2, 4, 3], [5, 3, 1, 2], [5, 3, 1, 4], [5, 3, 2, 1], [5, 3, 2, 4], [5, 3, 4, 1], [5, 3, 4, 2], [5, 4, 1, 2], [5, 4, 1, 3], [5, 4, 2, 1], [5, 4, 2, 3], [5, 4, 3, 1], [5, 4, 3, 2]]
[[None, 1, 2, 3], [None, 1, 3, 2], [None, 2, 1, 3], [None, 2, 3, 1], [None, 3, 1, 2], [None, 3, 2, 1], [1, None, 2, 3], [1, None, 3, 2], [1, 2, None, 3], [1, 2, 3, None], [1, 3, None, 2], [1, 3, 2, None], [2, None, 1, 3], [2, None, 3, 1], [2, 1, None, 3], [2, 1, 3, None], [2, 3, None, 1], [2, 3, 1, None], [3, None, 1, 2], [3, None, 2, 1], [3, 1, None, 2], [3, 1, 2, None], [3, 2, None, 1], [3, 2, 1, None]]
At each recursive call, if a combination has not yet been formed, the code does two things:
Checks if the specified number of buckets is greater than the input number list. If so, then the output is padded with None.
The remaining number list is iterated over, and each iteration value is added to the running result, and the recursion proceeds.
Use the built-in function itertools.permutations.
import itertools
def func(arr: list, slot_count=4):
if (v := slot_count - len(arr)) > 0:
arr.extend([None for _ in range(v)])
return itertools.permutations(arr, slot_count)
print(list(func([1, 2, 3])))
print(list(func([1, 2, 3, 4, 5])))
[(1, 2, 3, None), (1, 2, None, 3), (1, 3, 2, None), (1, 3, None, 2), (1, None, 2, 3), (1, None, 3, 2), (2, 1, 3, None), (2, 1, None, 3), (2, 3, 1, None), (2, 3, None, 1), (2, None, 1, 3), (2, None, 3, 1), (3, 1, 2, None), (3, 1, None, 2), (3, 2, 1, None), (3, 2, None, 1), (3, None, 1, 2), (3, None, 2, 1), (None, 1, 2, 3), (None, 1, 3, 2), (None, 2, 1, 3), (None, 2, 3, 1), (None, 3, 1, 2), (None, 3, 2, 1)]
[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 3), (1, 2, 4, 5), (1, 2, 5, 3), (1, 2, 5, 4), (1, 3, 2, 4), (1, 3, 2, 5), (1, 3, 4, 2), (1, 3, 4, 5), (1, 3, 5, 2), (1, 3, 5, 4), (1, 4, 2, 3), (1, 4, 2, 5), (1, 4, 3, 2), (1, 4, 3, 5), (1, 4, 5, 2), (1, 4, 5, 3), (1, 5, 2, 3), (1, 5, 2, 4), (1, 5, 3, 2), (1, 5, 3, 4), (1, 5, 4, 2), (1, 5, 4, 3), (2, 1, 3, 4), (2, 1, 3, 5), (2, 1, 4, 3), (2, 1, 4, 5), (2, 1, 5, 3), (2, 1, 5, 4), (2, 3, 1, 4), (2, 3, 1, 5), (2, 3, 4, 1), (2, 3, 4, 5), (2, 3, 5, 1), (2, 3, 5, 4), (2, 4, 1, 3), (2, 4, 1, 5), (2, 4, 3, 1), (2, 4, 3, 5), (2, 4, 5, 1), (2, 4, 5, 3), (2, 5, 1, 3), (2, 5, 1, 4), (2, 5, 3, 1), (2, 5, 3, 4), (2, 5, 4, 1), (2, 5, 4, 3), (3, 1, 2, 4), (3, 1, 2, 5), (3, 1, 4, 2), (3, 1, 4, 5), (3, 1, 5, 2), (3, 1, 5, 4), (3, 2, 1, 4), (3, 2, 1, 5), (3, 2, 4, 1), (3, 2, 4, 5), (3, 2, 5, 1), (3, 2, 5, 4), (3, 4, 1, 2), (3, 4, 1, 5), (3, 4, 2, 1), (3, 4, 2, 5), (3, 4, 5, 1), (3, 4, 5, 2), (3, 5, 1, 2), (3, 5, 1, 4), (3, 5, 2, 1), (3, 5, 2, 4), (3, 5, 4, 1), (3, 5, 4, 2), (4, 1, 2, 3), (4, 1, 2, 5), (4, 1, 3, 2), (4, 1, 3, 5), (4, 1, 5, 2), (4, 1, 5, 3), (4, 2, 1, 3), (4, 2, 1, 5), (4, 2, 3, 1), (4, 2, 3, 5), (4, 2, 5, 1), (4, 2, 5, 3), (4, 3, 1, 2), (4, 3, 1, 5), (4, 3, 2, 1), (4, 3, 2, 5), (4, 3, 5, 1), (4, 3, 5, 2), (4, 5, 1, 2), (4, 5, 1, 3), (4, 5, 2, 1), (4, 5, 2, 3), (4, 5, 3, 1), (4, 5, 3, 2), (5, 1, 2, 3), (5, 1, 2, 4), (5, 1, 3, 2), (5, 1, 3, 4), (5, 1, 4, 2), (5, 1, 4, 3), (5, 2, 1, 3), (5, 2, 1, 4), (5, 2, 3, 1), (5, 2, 3, 4), (5, 2, 4, 1), (5, 2, 4, 3), (5, 3, 1, 2), (5, 3, 1, 4), (5, 3, 2, 1), (5, 3, 2, 4), (5, 3, 4, 1), (5, 3, 4, 2), (5, 4, 1, 2), (5, 4, 1, 3), (5, 4, 2, 1), (5, 4, 2, 3), (5, 4, 3, 1), (5, 4, 3, 2)]
I have a np.array q with some values for example: [1,3,5,7] .
And a np.array z. with some values that I need to round and than they are used as index in the
Third array 'mapping'.
import numpy as np
q = [1,3,5,7]
z = [0,50.3,240.4,252.9,256]
mapping = np.zeros(256)
for i in range(len(q)):
print(i)
start, end = int(round(z[i])), int(round(z[i + 1]))
mapping[start:end] = int(round(q[i]))
print(mapping)
The output here is:
Here's my approach:
repeats = np.diff(list(np.round(z))+ [256]).astype(int)
# repeats = array([ 49, 191, 12, 3])
np.repeat(np.round(q), repeats)
Output:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7])
Note: this only has 255 elements and it's different from your expected output, because, tbh I don't really understand your logic.
I have a collection of arrays such as
a = [array([0, 1, 2, 3, 4]) array([0, 1, 2, 3]) array([0, 1, 2, 3, 4])
array([0, 1, 2, 3, 4, 5, 6, 7, 8]) array([0, 2, 3, 4, 5, 8, 9])
Is there any way to show this result is the form of key-value pairs like
[[(0),(1,2,3,4)],[(1),(0,2,3)],[(2),(0,1,3,4)],[(3),(0,1,2,4,5,6,7,8)]
[(4),(0,2,3,5,8,9)]]
i will get increment by 1 in the key and that value will be not included in the values list
I tried like this, but not able to put it into the required form.
c = [id for id in b if id != i] for i, b in enumerate(a)]
List comprehension with enumerate is one way. Note the comma after each single-item key. This represents that the object type is a tuple of length 1.
from numpy import array
a = [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3]), array([0, 1, 2, 3, 4]),
array([0, 1, 2, 3, 4, 5, 6, 7, 8]), array([0, 2, 3, 4, 5, 8, 9])]
res = [[(i,), tuple(j for j in arr if j != i)] for i, arr in enumerate(a)]
# [[(0,), (1, 2, 3, 4)],
# [(1,), (0, 2, 3)],
# [(2,), (0, 1, 3, 4)],
# [(3,), (0, 1, 2, 4, 5, 6, 7, 8)],
# [(4,), (0, 2, 3, 4, 8, 9)]]
Alternatively, you can create a dictionary:
res_dict = {i: tuple(j for j in arr if j != i) for i, arr in enumerate(a)}
# {0: (1, 2, 3, 4),
# 1: (0, 2, 3),
# 2: (0, 1, 3, 4),
# 3: (0, 1, 2, 4, 5, 6, 7, 8),
# 4: (0, 2, 3, 4, 8, 9)}
You can try numpy approach:
import numpy as np
a = [np.array([0, 1, 2, 3, 4]),np.array([0, 1, 2, 3]),np.array([0, 1, 2, 3, 4]),
np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]), np.array([0, 2, 3, 4, 5, 8, 9])]
print([[(i,),tuple(np.delete(j,np.argwhere(j==i)))] for i,j in enumerate(a)])
output:
[[(0,), (1, 2, 3, 4)], [(1,), (0, 2, 3)], [(2,), (0, 1, 3, 4)], [(3,), (0, 1, 2, 4, 5, 6, 7, 8)], [(4,), (0, 2, 3, 5, 8, 9)]]