Split lists into chunk based of index of another list - python

I want to split a list into chunks using values of of another list as the range to split.
indices = [3, 5, 9, 13, 18]
my_list = ['a', 'b', 'c', ..., 'x', 'y', 'z']
So basically, split my_list from range:
my_list[:3], mylist[3:5], my_list[5:9], my_list[9:13], my_list[13:18], my_list[18:]
I have tried to indices into chunks of 2 but the result is not what i need.
[indices[i:i + 2] for i in range(0, len(indices), 2)]
My actual list length is 1000.

You could also do it using simple python.
Data
indices = [3, 5, 9, 13, 18]
my_list = list('abcdefghijklmnopqrstuvwxyz')
Solution
Use list comprehension.
[(my_list+[''])[slice(ix,iy)] for ix, iy in zip([0]+indices, indices+[-1])]
Output
[['a', 'b', 'c'],
['d', 'e'],
['f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm'],
['n', 'o', 'p', 'q', 'r'],
['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']]
Check if correct order of indices are extracted
dict(((ix,iy), (my_list+[''])[slice(ix,iy)]) for ix, iy in zip([0]+indices, indices+[-1]))
Output
{(0, 3): ['a', 'b', 'c'],
(3, 5): ['d', 'e'],
(5, 9): ['f', 'g', 'h', 'i'],
(9, 13): ['j', 'k', 'l', 'm'],
(13, 18): ['n', 'o', 'p', 'q', 'r'],
(18, -1): ['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']}

Can use itertools.zip_longest
[my_list[a:b] for a,b in it.zip_longest([0]+indices, indices)]
[['a', 'b', 'c'],
['d', 'e'],
['f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm'],
['n', 'o', 'p', 'q', 'r'],
['s', 't', 'u', 'v', 'x', 'y', 'z']]
A little bit of code golf for fun:
map(my_list.__getitem__, map(lambda s: slice(*s), it.zip_longest([0]+indices, indices)))

One way using itertools.tee and pairwise:
from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
chunks = [my_list[i:j] for i, j in pairwise([0, *indices, len(my_list)])]
print(chunks)
Output:
[['a', 'b', 'c'],
['d', 'e'],
['f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm'],
['n', 'o', 'p', 'q', 'r'],
['s', 't', 'u', 'v', 'w', 'x', 'y', 'z']]
If numpy is an option, use numpy.array_split, which is meant for this:
import numpy as np
np.array_split(my_list, indices)
Output:
[array(['a', 'b', 'c'], dtype='<U1'),
array(['d', 'e'], dtype='<U1'),
array(['f', 'g', 'h', 'i'], dtype='<U1'),
array(['j', 'k', 'l', 'm'], dtype='<U1'),
array(['n', 'o', 'p', 'q', 'r'], dtype='<U1'),
array(['s', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='<U1')]

Related

How can I let my output be printed in a single string instead of seperate letters?

So I wrote this code to get every single row in a grid
def rows(test):
r = []
for x in test:
r.append(x)
return str(r)
the grid is this btw
test = [["r","a","w","b","i","t"],
["x","a","y","z","c","h"],
["p","q","b","e","i","e"],
["t","r","s","b","o","g"],
["u","w","x","v","i","t"],
["n","m","r","w","o","t"]]
and after running rows(test), I get this
[['r', 'a', 'w', 'b', 'i', 't'], ['x', 'a', 'y', 'z', 'c', 'h'], ['p', 'q', 'b', 'e', 'i', 'e'], ['t', 'r', 's', 'b', 'o', 'g'], ['u', 'w', 'x', 'v', 'i', 't'], ['n', 'm', 'r', 'w', 'o', 't']]
but I want it to be
[['rawbit','xayzch','pqbeie','trsbog', 'uwxvit', 'nmrwot']
What should I change??
Use .join:
def rows(test):
r = list()
for row in test:
r.append("".join(row))
return r
>>> rows(test)
['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot']
You can use ''.join() like below:
>>> lst = [['r', 'a', 'w', 'b', 'i', 't'], ['x', 'a', 'y', 'z', 'c', 'h'], ['p', 'q', 'b', 'e', 'i', 'e'], ['t', 'r', 's', 'b', 'o', 'g'], ['u', 'w', 'x', 'v', 'i', 't'], ['n', 'm', 'r', 'w', 'o', 't']]
>>> list(map("".join, lst))
#OR
>>> [''.join(l) for l in lst]
['rawbit', 'xayzch', 'pqbeie', 'trsbog', 'uwxvit', 'nmrwot']
If you want as function:
def rows(test):
# return [''.join(t) for t in test]
# Or
return list(map(''.join, test))

Sort a list by a key and sort another list in the same way as the first?

For example there are three lists:
unsorted_key = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
sorted_key = ['e', 'i', 'o', 'p', 'q', 'r', 't', 'u', 'w', 'y']
ciphertext = [
['u', 't', 'x', 'e'],
['p', 'r', 'k', 'p'],
['v', 'n', 'x', 'a'],
['n', 'h', 'e', 'x'],
['x', 'h', 'm', 's'],
['l', 'x', 'c', 'x'],
['x', 'c', 'y', 'a'],
['t', 'u', 'o', 'x'],
['e', 'r', 'm', 'e'],
['y', 'y', 'e', 'x']
]
Is it possible to take the order of the sorted_key and sort it into the unsorted_key, and take the order of the ciphertext and sort it in an identical way?
When moving 'q' from sorted_key[4] to sorted_key[0], it should move ciphertext[4] to ciphertext[0].
All three lists will always be of equal length.
The sorted_key and unsorted_key will never have repeating elements.
The sorted_key will always be a sorted version of unsorted_key.
I've been thinking about it and the only way I can think of would be to use a helper function to dynamically generate and return a lambda function from the order of unsorted_key, and then use something like:
sorted_key, ciphertext = (list(i) for i in zip(*sorted(zip(sorted_key, ciphertext), key=generate(unsorted_key))))
But I really don't know how zip() or lambda functions work or how to make a custom sorting order into one, or if one can even be returned to be used in sorted(). I really can't seem to wrap my head around this problem, so any help would be greatly appreciated!
An efficient approach to solve this problem in linear time is to create a dict that maps keys to indices of sorted_key, and then create a mappping dict that maps indices of unsorted_key to indices of sorted_key based on the same keys, so that you can iterate an index through the range of length of ciphertext to generate a list in the mapped order:
order = dict(map(reversed, enumerate(sorted_key)))
mapping = {i: order[k] for i, k in enumerate(unsorted_key)}
print([ciphertext[mapping[i]] for i in range(len(ciphertext))])
This outputs:
[['x', 'h', 'm', 's'], ['e', 'r', 'm', 'e'], ['u', 't', 'x', 'e'], ['l', 'x', 'c', 'x'], ['x', 'c', 'y', 'a'], ['y', 'y', 'e', 'x'], ['t', 'u', 'o', 'x'], ['p', 'r', 'k', 'p'], ['v', 'n', 'x', 'a'], ['n', 'h', 'e', 'x']]
The builtin sorted with a custom key can do it for you:
sorted(ciphertext, key=lambda x: unsorted_key.index(sorted_key[ciphertext.index(x)]))
Output:
[['x', 'h', 'm', 's'],
['e', 'r', 'm', 'e'],
['u', 't', 'x', 'e'],
['l', 'x', 'c', 'x'],
['x', 'c', 'y', 'a'],
['y', 'y', 'e', 'x'],
['t', 'u', 'o', 'x'],
['p', 'r', 'k', 'p'],
['v', 'n', 'x', 'a'],
['n', 'h', 'e', 'x']]
The lambda basically boils down to:
Find the current index
Find the value of current index in sorted_key
Find the index of sorted_key value in unsorted_key
Sort it
The one thing that I'm not clear about is why do you need to "sort" sorted_key if the end result is identical to unsorted_key? Just sorted_key = unsorted_key[:] is simple enough if that's the case. But if you really need to sort sorted_key as well, you can do this (it would actually make the lambda simpler):
ciphertext, sorted_key = map(list, zip(*sorted(zip(ciphertext, sorted_key), key=lambda x: unsorted_key.index(x[1]))))
ciphertext
[['x', 'h', 'm', 's'],
['e', 'r', 'm', 'e'],
['u', 't', 'x', 'e'],
['l', 'x', 'c', 'x'],
['x', 'c', 'y', 'a'],
['y', 'y', 'e', 'x'],
['t', 'u', 'o', 'x'],
['p', 'r', 'k', 'p'],
['v', 'n', 'x', 'a'],
['n', 'h', 'e', 'x']]
sorted_key
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
I'm not sure I get the point, but...
First determine the moves (can be the opposite, it0s not clear to me):
moves = [ [i, sorted_key.index(c)] for i, c in enumerate(unsorted_key) ]
#=> [[0, 4], [1, 8], [2, 0], [3, 5], [4, 6], [5, 9], [6, 7], [7, 1], [8, 2], [9, 3]]
Maybe swap elements in [i, sorted_key.index(c)].
Apply the moves to a receiver (res):
res = [ None for _ in range(len(ciphertext))]
for a, b in moves:
res[a] = ciphertext[b]
So the output should be:
for line in res:
print(line)
# ['x', 'h', 'm', 's']
# ['e', 'r', 'm', 'e']
# ['u', 't', 'x', 'e']
# ['l', 'x', 'c', 'x']
# ['x', 'c', 'y', 'a']
# ['y', 'y', 'e', 'x']
# ['t', 'u', 'o', 'x']
# ['p', 'r', 'k', 'p']
# ['v', 'n', 'x', 'a']
# ['n', 'h', 'e', 'x']
For testing execution time
import timeit, functools
def custom_sort(ciphertext, sorted_key, unsorted_key):
return [ ciphertext[b] for _, b in [ [i, sorted_key.index(c)] for i, c in enumerate(unsorted_key) ] ]
custom_sort = timeit.Timer(functools.partial(custom_sort, ciphertext, sorted_key, unsorted_key))
print(custom_sort.timeit(20000))
I'm not sure I'm understanding your question properly, but if you're attempting to sort the unsorted key, and ensure that the ciphertexts are sorted accordingly, this should do what you want:
pairs = zip(unsorted_key, ciphertext)
sorted_key = []
sorted_ciphertexts = []
for t in sorted(pairs):
sorted_key.append(t[0])
sorted_ciphertexts.append(t[1])
I'm sure there's probably a more elegant way to do it, but this will ensure that the key and ciphertexts are placed at the same index.

Select n items by sequence of a list repeatedly in python

Say I have a long list:
>>> import string
>>> my_list = list(string.ascii_lowercase)
>>> my_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
I want to loop over this list and select n items by sequence repeatedly. E.g. if I want to select 5 items, then it should be like:
step 1: ['a', 'b', 'c', 'd', 'e']
step 2: ['f', 'g', 'h', 'i', 'j']
step 3: ['k', 'l', 'm', 'n', 'o']
step 4: ['p', 'q', 'r', 's', 't']
step 5: ['u', 'v', 'w', 'x', 'y']
step 6: ['z', 'a', 'b', 'c', 'd']
step 7: ['e', 'f', 'g', 'h', 'i']
......
So the point is: I want to make sure when I reach the last item of the list, the first items can be appended to the last one and the looping just keep going.
For appending the first items to the last items, I've tried something like this:
def loop_slicing(lst_, i):
""" Slice iterable repeatedly """
if i[0] > i[1]:
return [n for n in lst_[i[0]:]+lst_[:i[1]]]
else:
return lst_[i[0]:i[1]]
When I call this function, I can do this:
>>> loop_slicing(my_list, (0, 5))
['a', 'b', 'c', 'd', 'e']
>>> loop_slicing(my_list, (25, 4))
['z', 'a', 'b', 'c', 'd']
Where I can just make a generator which can generate 5 sequential numbers in range(0, 26) to loop over my_list and get 5 items each time.
I don't know if this is the best approach. So is there any more efficient way to do the stuff?
Using the itertools module you can cycle and slice a string via an infinite generator:
from itertools import cycle, islice
from string import ascii_lowercase
def gen(x, n):
c = cycle(x)
while True:
yield list(islice(c, n))
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
...
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
Debatably simpler solution using a list comprehension:
def batch_list(ns, batch_size):
return [ns[i:i+batch_size] for i in range(0, len(ns), batch_size)]
>>> batch_list('abcdefghijk', 3)
['abc', 'def', 'ghi', 'jk']
This is a simple construction that I find myself writing often when I want to batch some list of tasks to perform.
EDIT: Just realized the OP asked for the construction to cycle around to the beginning to complete the last batch if needed. This does not do that and will have the last batch truncated.
Thanks for asking,
I took some time to understand the objective of your algorithm but if you want to loop and save all of your sublists I think this should work :
def slicing_items(slc_len = 5, lst, iterate_num = 25):
# slc_len correspond to the number of slices, lst is the list of sequences
n = len(lst)
k = 1
p = k * slc_len
slicing_list = []
while k < iterate_num:
current_slice = []
if p >= n:
for i in range (1, p//n):
current_slice += lst #How many times we passed the length of the list
p = p % n #How many items remaining ?
current_slice += lst[-(slc_len-p):]
current_slice += lst[:p]
else:
current_slice = lst[p-slc_len:p]
k += 1
p += slc_len
slicing_list.append(current_slice)
return slicing_list
Output :
slicing_items(5,my_list,10)
>>> [['a', 'b', 'c', 'd', 'e'],
['f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm', 'n', 'o'],
['p', 'q', 'r', 's', 't'],
['u', 'v', 'w', 'x', 'y'],
['z', 'a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i'],
['j', 'k', 'l', 'm', 'n'],
['o', 'p', 'q', 'r', 's']]
However if you just want the last slice over your iterate_num then your function should fit perfectly (maybe you should use slicing over than rewriting the list in your first boolean statement for rapidity)
Using generator and slicing:
from string import ascii_lowercase
def gen(x, n):
start, stop = 0, n
while True:
if start < stop:
yield list(x[start:stop])
else:
yield ((list(x[start:])) + (list(x[:stop])))
start = stop
stop = (stop + n) % len(x)
G = gen(ascii_lowercase, 5)
print(next(G)) # ['a', 'b', 'c', 'd', 'e']
print(next(G)) # ['f', 'g', 'h', 'i', 'j']
print(next(G))
print(next(G))
print(next(G)) # ['u', 'v', 'w', 'x', 'y']
print(next(G)) # ['z', 'a', 'b', 'c', 'd']
print(next(G))
OUTPUT :
['a', 'b', 'c', 'd', 'e']
['f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o']
['p', 'q', 'r', 's', 't']
['u', 'v', 'w', 'x', 'y']
['z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i']
This is a really interesting problem , if you want to "just" solve the problem go for itertools cycle approach , It have already in-built function, But if you want to enjoy the joy of algorithms building, Go with your own solution and try something :
Here i tried with recursion approach, As you said it will keep going so you have to handle recursion by setting your max limit:
import math
import sys
sys.setrecursionlimit(500)
data=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
try:
def recursive_approch(m, x, n, hold=0, value=0, left=0):
print(hold)
max_time = len(m) / n
max_t = int(math.modf(max_time)[1])
left = len(m) % n
if value == max_t:
if len(x) == left:
x = x + m[:-len(x)]
value = 0
left = 0
else:
hold = x[:n]
value = value + 1
return recursive_approch(m, x[n:], n, hold=hold, value=value, left=left)
return recursive_approch(m, x, n, hold=hold, value=value, left=left)
print(recursive_approch(data, data, 6))
except RecursionError:
print('maximum recursion')
You have to pass the number for slice so if you want to slice 6-6 then:
print(recursive_approch(data, data, 6))
output:
['a', 'b', 'c', 'd', 'e', 'f']
['g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r']
['s', 't', 'u', 'v', 'w', 'x']
['s', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p']
['q', 'r', 's', 't', 'u', 'v']
['q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd']
['e', 'f', 'g', 'h', 'i', 'j']
...................
If you want 3-3 then:
['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h', 'i']
['j', 'k', 'l']
['m', 'n', 'o']
['p', 'q', 'r']
['s', 't', 'u']
['v', 'w', 'x']
['v', 'w', 'x']
['y', 'z', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
['e', 'f', 'g']
['h', 'i', 'j']
['k', 'l', 'm']
['n', 'o', 'p']
['q', 'r', 's']
['t', 'u', 'v']
['t', 'u', 'v']
['w', 'x', 'a']
['b', 'c', 'd']
.......
if you pass 12 :
0
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['w', 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v']
....

Python - itertools.product without using element more than once

I have a list of lists, and I would like to duplicate the effect of itertools.product() without using any element more than once.
>>> list = [['A', 'B'], ['C', 'D'], ['A', 'B']]
>>> [''.join(e) for e in itertools.product(*list)]
['ACA', 'ACB', 'ADA', 'ADB', 'BCA', 'BCB', 'BDA', 'BDB']
>>> # Desired output: ['ACB', 'ADB', 'BCA', 'BDA']
The list I need to use this on is too large to compute itertools.product and remove the unneeded elements. (25 billion permutations from itertools.product, while my desired output only has ~500,000). Preferably, an answer will be iterable.
Edit: I know "product" is the wrong term for what I need, but I'm struggling to find the word I'm looking for.
Edit2: This is the list that I desire to perform this operation on:
[['A', 'H', 'L'], ['X', 'B', 'I'], ['Q', 'C', 'V'], ['D', 'N'], ['E', 'F'], ['E', 'F'], ['G'], ['A', 'H', 'L'], ['X', 'B', 'I'], ['W', 'U', 'J', 'K', 'M'], ['W', 'U', 'J', 'K', 'M'], ['A', 'H', 'L'], ['W', 'U', 'J', 'K', 'M'], ['D', 'N'], ['P', 'O', 'T'], ['P', 'O', 'T'], ['Q', 'C', 'V'], ['R'], ['S'], ['P', 'O', 'T'], ['W', 'U', 'J', 'K', 'M'], ['Q', 'C', 'V'], ['W', 'U', 'J', 'K', 'M'], ['X', 'B', 'I']]
A simple stack-based implementation:
def product1(l): return product1_(l,0,[])
def product1_(l,i,buf):
if i==len(l): yield buf
else:
for x in l[i]:
if x not in buf:
buf.append(x)
yield from product1_(l,i+1,buf)
buf.pop()
This is a bit slower than Patrick Haugh's answer (I get 18 s for your test case), but it gives the results in a predictable order.
Note that you have to process the values as it generates "them", since they're all the same list buf; you could write yield tuple(buf) or yield "".join(buf) to generate separate "cooked" values (at a cost of less than one additional second).
If the values are letters, you could use a "bitmask" list to implement the collision test, which reduces the time to about 13 s (but using a set is just as fast). Other possible optimizations include processing lists with fewer eligible elements first, to reduce backtracking; this can get this case down to 11 s.
test1 = [['A', 'B'], ['C', 'D'], ['A', 'B']]
test2 = [['A', 'H', 'L'], ['X', 'B', 'I'], ['Q', 'C', 'V'], ['D', 'N'], ['E', 'F'], ['E', 'F'], ['G'], ['A', 'H', 'L'],
['X', 'B', 'I'], ['W', 'U', 'J', 'K', 'M'], ['W', 'U', 'J', 'K', 'M'], ['A', 'H', 'L'],
['W', 'U', 'J', 'K', 'M'], ['D', 'N'], ['P', 'O', 'T'], ['P', 'O', 'T'], ['Q', 'C', 'V'], ['R'], ['S'],
['P', 'O', 'T'], ['W', 'U', 'J', 'K', 'M'], ['Q', 'C', 'V'], ['W', 'U', 'J', 'K', 'M'], ['X', 'B', 'I']]
def prod(curr, *others):
if not others:
for x in curr:
yield {x} # (x,) for tuples
else:
for o in prod(*others):
for c in curr:
if c not in o:
yield {c, *o} # (c, *o) for tuples
print([''.join(x) for x in prod(*test1)])
# ['ABC', 'ABD', 'ABC', 'ABD']
print(sum(1 for x in prod(*test2)))
# 622080
The longer input takes about five seconds to run on my machine. I use sets to pass values around because they are much more efficent than tuples or lists when it comes to membership checks. If necessary, you can use tuples, it will just be slower.
Some questions to think about: does order matter? What do you want to happen when we can't use an item from the current list (because they've all already been used)?
Your specific case has an interesting property. If we arrange it in a counter, we see that every list occurs as many times as its entries:
Counter({('A', 'H', 'L'): 3,
('D', 'N'): 2,
('E', 'F'): 2,
('G',): 1,
('P', 'O', 'T'): 3,
('Q', 'C', 'V'): 3,
('R',): 1,
('S',): 1,
('W', 'U', 'J', 'K', 'M'): 5,
('X', 'B', 'I'): 3})
In other words, ignoring order, the sequences you want are the cartesian products of permutations of your lists. Suppose your list is called l. Then we can assemble a list of all the permutations of the sublists and take their product:
c = set(tuple(i) for i in l)
permutations = [list(itertools.permutations(i)) for i in c]
permutation_products = itertools.products(*permutations)
An element of permutation_products looks something like:
(('W', 'U', 'J', 'K', 'M'),
('E', 'F'),
('X', 'B', 'I'),
('Q', 'C', 'V'),
('P', 'O', 'T'),
('D', 'N'),
('G',),
('S',),
('R',),
('A', 'L', 'H'))
We have to get it back into the right order. Suppose our permutation is called perm. For each sublist of your list, we have to locate the correct element of perm and then take the next letter in the permutation. We can do this by making a dictionary:
perm_dict = {frozenset(p): list(p) for p in perm}
Then, to construct a single permutation, we have:
s = "".join([perm_dict[frozenset(i)].pop() for i in l])
We can combine all this into a generator:
def permute_list(l):
c = set(tuple(i) for i in l)
permutations = [list(itertools.permutations(i)) for i in c]
permutation_products = itertools.product(*permutations)
for perm in permutation_products:
perm_dict = {frozenset(p): list(p) for p in perm}
yield "".join([perm_dict[frozenset(i)].pop() for i in l])

Python: Iterating through the columns in a list of list to find the palindromes

Here I have a word list as:
[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
And I have to display all the palindromes in this list which are in rows as well as columns.
I have coded to find all the palindromes in the rows. But cannot implement a method to find the palindromes in the columns.
Here is my code so far:
result_1=""
if len(palindrome)==len_line_str:
for row in range(len(palindrome)):
for horizontal_line in range(len(palindrome[row])):
if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
result_1=''.join(palindrome[row])+" is a palindrome starting at ["+str(row)+"]["+str(row)+"] and is a row in the table"
print(result_1)
Which will display the output:
rotor is a palindrome starting at [0][0] and is a row in the table
Where "rotor" is a palindrome.
I need a method to get the palindromes in the columns which are:
"refer", "tenet", "radar"
Any help is much appreciated. Thanks in advance!
You can use zip to transpose your lists:
>>> t = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> list(zip(*t))
[('r', 'e', 'f', 'e', 'r'), ('o', 'v', 'i', 'n', 'a'), ('t', 'e', 'n', 'e', 't'), ('o', 'i', 'e', 't', 'e'), ('r', 'a', 'd', 'a', 'r')]
Your columns are now rows, and you can apply the same method than before. If you just need the words, you can use list comprehensions:
>>> rows = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> [''.join(row) for row in rows if row[::-1] == row ]
['rotor']
>>> [''.join(column) for column in zip(*rows) if column[::-1] == column ]
['refer', 'tenet', 'radar']
This will do the job:
palindrome=[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
n=len(palindrome)
for col in range(len(palindrome[0])):
col_word=[palindrome[i][col] for i in range(n)]
if ''.join(col_word)==''.join(reversed(col_word)):
result=''.join(col_word)+" is a palindrome starting at ["+str(col)+"] and is a col in the table"
print(result)
This prints
refer is a palindrome starting at [0] and is a col in the table
tenet is a palindrome starting at [2] and is a col in the table
radar is a palindrome starting at [4] and is a col in the table
Basically, in order to access the words in the column, you can do
col_word=[palindrome[i][col] for i in range(n)]
This fixes the column and iterates over the rows. The rest of the code is structures similarly to yours.
​
I saw you did not want to use Zip (which I would recommend using):
Alternative answer:
list_ = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
You can get the palindromes (rows) by checking each list with the reversed list [::-1]:
[i==i[::-1] for i in list_]
# prints [True, False, False, False, False]
And get the palindromes (columns) by 1. create the column list (called list_2 below) with a list comprehension and 2. same principle as above:
list_2 = [[i[ind] for i in list_] for ind in range(len(list_))]
[i==i[::-1] for i in list_2]
# prints [True, False, True, False, True]
Update
If you want the answers directly you can do:
[i for i in list_ if i==i[::-1]]
# prints [['r', 'o', 't', 'o', 'r']]
# and list_2: [['r', 'e', 'f', 'e', 'r'],['t', 'e', 'n', 'e', 't'],['r', 'a', 'd', 'a', 'r']]
There are a lot of ways to do it. I will take as example your code because of your effort on it
Another alternative following your code, is creating the columns in another list and check wich of them are palindromes:
palindrome = [['r', 'o', 't', 'o', 'r'],
['e', 'v', 'e', 'i', 'a'],
['f', 'i', 'n', 'e', 'd'],
['e', 'n', 'e', 't', 'a'],
['r', 'a', 't', 'e', 'r']]
len_line_str = 5
result_1=""
def is_pal(string):
return string == reversed(string)
colums = []
if len(palindrome)==len_line_str:
for row in range(len(palindrome)):
vertical = []
if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
result_1+=''.join(palindrome[row])+" is a palindrome starting at ["+str(0)+"]["+str(row)+"] and is a row in the table. " + "\n"
for horizontal_line in range(len(palindrome[row])):
if(len_line_str-1 > horizontal_line):
vertical += [palindrome[horizontal_line][row]]
else:
vertical += [palindrome[horizontal_line][row]]
colums += [(vertical,row)]
for word in colums:
if ''.join(word[0])==''.join(reversed(word[0])):
result_1+=''.join(word[0])+" is a palindrome starting at ["+str(0)+"]["+str(word[1])+"] and is a column in the table" + "\n"
print(result_1)
This should work. First loop iterates through the list s and the second loop iterates through each list.
Assuming s is the name of the list- [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
for i in xrange(0,len(s),1):
str = ""
for j in s:
str = str + j[i]
print str
if str == str[::-1]:
print str," is a pallindrome - column", i
else:
print str," is not a pallindrome - column", i
There is no column wise traversal in Python. One hacky way you can follow is to perform transpose operation on your input matrix. Below is a simple way to implement transpose using list comprehensions.
def transpose(matrix):
if not matrix:
return []
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
Your same logic should work once modify your input using transpose.
Hope this helps!!

Categories