Related
I have a list of tuples
a=[('a', 0), ('c', 1), ('d', 0), ('b', 1), ('t',1), ('j',2), ('k',3), ('s', 4), ('l',1), ('y',1), ('r',2), ('b',3), ('k',4)]
I want output like
[[1,1,1,2,3,4],[1,1,2,3,4]]
and corresponding letters
[['c', 'b', 't', 'j', 'k', 's'], ['l', 'y', 'r', 'b', 'k']]
I need to remove 0's in between and the pattern always starts with 1
Using a simple loop and tracking the previous non-zero value:
letters = []
numbers = []
prev = 2
for l,n in a:
if n == 0:
continue
elif prev > 1 and n == 1:
letters.append([])
numbers.append([])
letters[-1].append(l)
numbers[-1].append(n)
prev = n
letters
# [['c', 'b', 't', 'j', 'k', 's'], ['l', 'y', 'r', 'b', 'k']]
numbers
# [[1, 1, 1, 2, 3, 4], [1, 1, 2, 3, 4]]
So I am rearranging a list based on an index pattern and would like to find a way to calculate the pattern I need to revert the list back to its original order.
for my example I am using a list of 5 items as I can work out the pattern needed to revert the list back to its original state.
However this isn't so easy when dealing with 100's of list items.
def rearrange(pattern: list, L: list):
new_list = []
for i in pattern:
new_list.append(L[i-1])
return new_list
print(rearrange([2,5,1,3,4], ['q','t','g','x','r']))
#['t', 'r', 'q', 'g', 'x']
and in order to set it back to the original pattern
I would use
print(rearrange([3,1,4,5,2],['t', 'r', 'q', 'g', 'x']))
#['q', 't', 'g', 'x', 'r']
What I am looking for is a way to calculate the pattern "[3,1,4,5,2]"
regarding the above example.
whist running the script so that I can set the list back to its original order.
Using a larger example:
print(rearrange([18,20,10,11,13,1,9,12,16,6,15,5,3,7,17,2,19,8,14,4],['e','p','b','i','s','r','q','h','m','f','c','g','d','k','l','t','a','n','j','o']))
#['n', 'o', 'f', 'c', 'd', 'e', 'm', 'g', 't', 'r', 'l', 's', 'b', 'q', 'a', 'p', 'j', 'h', 'k', 'i']
but I need to know the pattern to use with this new list in order to return it to its original state.
print(rearrange([???],['n', 'o', 'f', 'c', 'd', 'e', 'm', 'g', 't', 'r', 'l', 's', 'b', 'q', 'a', 'p', 'j', 'h', 'k', 'i']))
#['e','p','b','i','s','r','q','h','m','f','c','g','d','k','l','t','a','n','j','o']
This is commonly called "argsort". But since you're using 1-based indexing, you're off-by-one. You can get it with numpy:
>>> pattern
[2, 5, 1, 3, 4]
>>> import numpy as np
>>> np.argsort(pattern) + 1
array([3, 1, 4, 5, 2])
Without numpy:
>>> [1 + i for i in sorted(range(len(pattern)), key=pattern.__getitem__)]
[3, 1, 4, 5, 2]
What about something like below:
def revert_pattern(pattern):
pattern_i = [0]*len(pattern)
for k in range(len(pattern)):
pattern_i[pattern[k]-1] = k+1
return pattern_i
print(revert_pattern([2, 5, 1, 3, 4]))
# [3, 1, 4, 5, 2]
Note: I followed your logic but I recommend you using 0 as the smallest indexes instead of 1 since it requires somes extra +1/-1 that could be avoided
def rearrange(p, l):
arr = [l[i - 1] for i in p]
d = {v : i + 1 for i, v in enumerate(arr)}
order = [d[k] for k in l]
return arr, order
a = [2, 5, 1, 3, 4]
b = ['q', 't', 'g', 'x', 'r']
rearrange(a, b)
# (['t', 'r', 'q', 'g', 'x'], [3, 1, 4, 5, 2])
OR maybe
def revert(p):
z = zip(p, list(range(len(p))))
return [x + 1 for _, x in sorted(z)]
a = [2, 5, 1, 3, 4]
revert(a)
# [3, 1, 4, 5, 2]
I am looking for a way to obtain combinations of single elements of all sub-lists contained in a list without knowing in advance the length of the list and the sub-lists. Let me illustrate what I mean via two examples below. I have two lists (myList1 and myList2) and would like to obtain the two combination sets (setsCombo1 and setsCombo1):
myList1 = [['a'], [1, 2, 3], ['X', 'Y']]
setsCombo1 = [['a', 1, 'X'],
['a', 1, 'Y'],
['a', 2, 'X'],
['a', 2, 'Y'],
['a', 3, 'X'],
['a', 3, 'Y']]
myList2 = [['a'], [1, 2, 3], ['X', 'Y'], [8, 9]]
setsCombo2 = [['a', 1, 'X', 8],
['a', 1, 'X', 9],
['a', 1, 'Y', 8],
['a', 1, 'Y', 9],
['a', 2, 'X', 8],
['a', 2, 'X', 9],
['a', 2, 'Y', 8],
['a', 2, 'Y', 9],
['a', 3, 'X', 8],
['a', 3, 'X', 9],
['a', 3, 'Y', 8],
['a', 3, 'Y', 9]]
I looked a bit into itertools but couldn't really find anything quickly that is appropriate...
itertools.product with unpacking * (almost) does that:
>>> from itertools import product
>>> list(product(*myList1))
[('a', 1, 'X'),
('a', 1, 'Y'),
('a', 2, 'X'),
('a', 2, 'Y'),
('a', 3, 'X'),
('a', 3, 'Y')]
To cast the inner elements to lists, we map:
>>> list(map(list, product(*myList1)))
[['a', 1, 'X'],
['a', 1, 'Y'],
['a', 2, 'X'],
['a', 2, 'Y'],
['a', 3, 'X'],
['a', 3, 'Y']]
How one might get Sympy Permutation to act on a list? E.g.,
from sympy.combinatorics import Permutation
lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
# Then something like...
perm * lst # This doesn't work. Throws AttributeError because of list
I'd like something like this that returns (in this example):
['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']
I have read https://docs.sympy.org/latest/modules/combinatorics/permutations.html, and don't see how.
Any suggestions as to how might one go about this?
You can just do perm(lst)
>>> from sympy.combinatorics import Permutation
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
>>> perm(lst)
['c', 'f', 'i', 'b', 'e', 'h', 'a', 'd', 'g']
Your example output seems to have the result of applying the reverse of the given Permutation to the list - if that is your required output you need to either reverse the final list or each list within the permutation.
From here:
The permutation can be ‘applied’ to any list-like object, not only Permutations.
I have this data structure:
It is 2d-array that is divided on 3 sections. For each letter in the array I need to define Section number. For example, letters a,b,c,d are in Section 1; e,f,g,h are in Section 2.
My code. Firstly, this 2d-array preparation:
from itertools import cycle
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
#2d-array initialization
width, height = 3, 6
repRange = cycle(range(1, 3))
values = [0] * (width - 1)
array2d = [[next(repRange)] + values for y in range(height)]
#Filling array with letters:
m = 0
for i in range(height):
for j in range(1,width):
array2d[i][j] = letters[m]
m+=1
#Printing:
for row in array2d:
print(row)
Output:
[1, 'a', 'b']
[2, 'c', 'd']
[1, 'e', 'f']
[2, 'g', 'h']
[1, 'i', 'j']
[2, 'k', 'l']
Now I need to determine section number of each letter and save it along with the letter itself. I use defineSection function and save values in dictionary:
def defineSection(i, division, height):
if i <= division:
return 1
elif division*2 >= i > division :
return 2
elif division*3 >= i > division*2 :
return 3
dic = {}
for i in range(height):
for j in range(1,width):
section = defineSection(i+1, 2, height)
dic.update({array2d[i][j] : section})
for item in dic.items():
print(item)
Output:
('f', 2)
('b', 1)
('c', 1)
('e', 2)
('k', 3)
('g', 2)
('d', 1)
('a', 1)
('l', 3)
('h', 2)
('i', 3)
('j', 3)
It defined all section numbers for each letter correctly. But defineSection method is primitive and will not work if number of rows is bigger than 6.
I don't know how to implement defineSection method so that it defines Section number automatically taking into account only current Row number, division and number of rows in total.
Question: Is there some way I can simply determine section number without so many if-elif conditions and independently of total number of rows?
You can simplify your matrix creation code immensely. All you need is a letters iterator, which returns itself so you can iterate 2-letters at a time using zip.
In [3]: from itertools import cycle
In [4]: letters = "abcdefghijkl"
In [5]: ranges = cycle(range(1,3))
In [6]: iter_letters = iter(letters)
In [7]: matrix = [[i,a,b] for i,a,b in zip(ranges,iter_letters,iter_letters)]
In [8]: matrix
Out[8]:
[[1, 'a', 'b'],
[2, 'c', 'd'],
[1, 'e', 'f'],
[2, 'g', 'h'],
[1, 'i', 'j'],
[2, 'k', 'l']]
As for assigning sections, note that a section is every two rows, which is four letters, so you can use simple floor division to "skip" counts.
In [9]: sections = {letter:(i//4 + 1) for i,letter in enumerate(letters)}
In [10]: sections
Out[10]:
{'a': 1,
'b': 1,
'c': 1,
'd': 1,
'e': 2,
'f': 2,
'g': 2,
'h': 2,
'i': 3,
'j': 3,
'k': 3,
'l': 3}