Home > Mobile >  python selecting by rotation from multiple lists
python selecting by rotation from multiple lists

Time:09-30

i have 4 lists as follows:

import numpy as np

file1 = np.arange(0, 4).tolist()
file2 = np.arange(3, 7).tolist()
file3 = np.arange(7, 11).tolist()
file4 = np.arange(11, 15).tolist()

and i want to select by rotation from each list and end up with a final set as follows:

final = {0, 4, 9, 14}

i have a solution that does work but i am wondering if there is an easier method or a more pythonic way to do this?

def get_rotated(file1: list,
                         file2: list,
                         file3: list,
                         file4: list):
    count = 0
    i = 0    
    final = set()    
    while i < (get_len(file1) -1):
        print('here')
        if count == 0:
            print(i)
            tag = file1[i]
            count =1
            final.add(tag)
            i  = 1
        if count == 1:
            print(i)

            tag = file2[i]
            count =1
            final.add(tag)
            i  = 1
        if count == 2:
            print(i)
            tag = file3[i]
            count =1
            final.add(tag)
            i  = 1    
        if count == 3:
            print(i)
            tag = file4[i]
            count=0
            final.add(tag)
            i  = 1
    return final

this returns the correct set {0, 4, 9, 14}

CodePudding user response:

This is what you need. You should use list names like below via iterating

import numpy as np

file1 = np.arange(0, 4).tolist()
file2 = np.arange(3, 7).tolist()
file3 = np.arange(7, 11).tolist()
file4 = np.arange(11, 15).tolist()

countOfLists = 4
finalList = []
for i in range(1, countOfLists   1):
    finalList.append(globals()['file%s'%i][i - 1])
print(finalList)

CodePudding user response:

You could make a list of lists and pick the item corresponding to each list's position in the list of lists:

[ L[i] for i,L in enumerate([file1,file2,file3,file4]) ]

[0, 4, 9, 14]
  • Related