-3

I have searched everywhere on how to rotate/shift each element of a list simultaneously (using different speed) but nothing is found. the solutions in the following links only shift the entire list or doesn't solve my issue:

Efficient way to rotate a list in python

https://www.geeksforgeeks.org/python-ways-to-rotate-a-list/

How to iterate all item at once in python list?

This is what am trying:

test_list = ['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy']

def idx_1():
    for i in range(10):
        a1 = list(test_list[0])
        for i in range(len(a1)):
            a1.append(a1.pop(0))  
            pkidx_1 = "".join(a1)    
            time.sleep(0)       #to control/determine speed of iteration
            print(pkidx_1)

def idx_2():
    for i in range(10):
        a2 = list(test_list[1])
        for i in range(len(a2)):
            a2.append(a2.pop(0))
            pkidx_2 = "".join(a2) 
            time.sleep(0.0001)
            print(pkidx_2)  

def idx_3():
    for i in range(10):
        a3 = list(test_list[2])
        for i in range(len(a3)):
            a3.append(a3.pop(0))
            pkidx_3 = "".join(a3) 
            time.sleep(0.0002)
            print(pkidx_3)

def idx_4():
    for i in range(10):
        a4 = list(test_list[3])
        for i in range(len(a4)):
            a4.append(a4.pop(0))
            pkidx_4 = "".join(a4) 
            time.sleep(0.0003)
            print(pkidx_4)  

def idx_5():
    for i in range(10):
        a5 = list(test_list[4])
        for i in range(len(a5)):
            a5.append(a5.pop(0))
            pkidx_5 = "".join(a5) 
            time.sleep(0.0004)
            print(pkidx_5)


if __name__=='__main__':
    p1 = threading.Thread(target=idx_1)
    p2 = threading.Thread(target=idx_2)
    p3 = threading.Thread(target=idx_3)
    p4 = threading.Thread(target=idx_4)
    p5 = threading.Thread(target=idx_5)
    p1.start();p2.start();p3.start();p4.start();p5.start()

But i get stucked merging all the output to emit as a single list/variable. Expected output:

['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy']
['bcdea', 'ghijf', 'lmnok', 'qrstp', 'vwxyu']
['cdeab', 'hijfg', 'mnokl', 'rstpq', 'wxyuv']
['deabc', 'ijfgh', 'noklm', 'stpqr', 'xyuvw']
['eabcd', 'jfghi', 'oklmn', 'tpqrs', 'yuvwx']
['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy']

The #time.sleep may influence the result though i.e by the time idx_5 (slowest def) finish its first iteration, the faster would have cycle more times and so on.

Thanks in advance.

7r0jan005
  • 31
  • 7
  • 1
    Actually, I don't now know what your question is. Undoubtedly your method can be improved by using `np.roll()` but you appear to actually be asking about `sleep()`? I'm confused – roganjosh Nov 12 '22 at 08:21
  • "merging all the output to emit as a single list/variable" well, that's not what your expected output actually _is_. What you have is a series of lists, but no enclosing list. At best it would be a printout or lines in a file, but it's not syntactically valid in python _as a whole_ unless you have an enclosing list – roganjosh Nov 12 '22 at 08:22
  • 1
    But that isn't the question title. And you give absolutely no indication of what the issue is with the speed of the rotation or what you actually want in that regard – roganjosh Nov 12 '22 at 08:36
  • I can't fix something I can't see. There is no reason the output should be static if you made your loop correctly. That also wasn't part of your question – roganjosh Nov 13 '22 at 12:49

1 Answers1

1

You can do this using np.roll

import numpy as np

test_list = ['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy']


def roll(lst, shift_factor):
    arr = np.array([list(item) for item in lst])
    arr = np.roll(arr, shift_factor, axis=1)
    return [''.join(sublist) for sublist in arr]

 
new_list = roll(test_list, -1)
print(new_list)

Gives:

['bcdea', 'ghijf', 'lmnok', 'qrstp', 'vwxyu']

Then you just change the shift_factor as needed

roganjosh
  • 12,594
  • 4
  • 29
  • 46