2

I have the following list: ['123456789', '234567891', '345678912', '456789123', '567891234', '789123456', '891234567', '912345678']

I am trying to create 9 separate list from this one, each containing the first, second, third element of each of the elements of this list. The list should contain ints,for example the first two lists would be:

[1,2,3,4,5,6,7,8,9]
[2,3,4,5,6,7,8,9,1]

I realise the two lists are just the first two elements of the main lists with ints. But I'm trying to create them from the only the first&second element of each of the elements in the big list, so I think some for loop is needed.

daniel stafford
  • 241
  • 1
  • 8

5 Answers5

2
startList = ['123456789', '234567891', '345678912', '456789123', '567891234', '789123456', '891234567', '912345678']
finalLists = list(map(lambda x: list(map(lambda y: int(y), list(x))), startList))
print(finalLists)
Micos
  • 87
  • 9
  • Thanks, this works but has there got a way to do it with for loops? – daniel stafford Sep 18 '22 at 10:48
  • 1
    yes, map() function is literally a one-line loop, if you want ill write that as a loops – Micos Sep 18 '22 at 10:50
  • Note that this only happens to work because the rows and columns in the original 2D list has the same elements in its rows as in its columns. This doesn't appear to acctually transpose the matrix. – Code-Apprentice Dec 11 '22 at 19:01
2

Using loops:

startList = ['123456789', '234567891', '345678912', '456789123', '567891234', '789123456', '891234567', '912345678']
finalLists = []
for splittingList in startList:
    workingList = []
    for character in splittingList:
       workingList.append(int(character))
    finalLists.append(workingList)

print(finalLists)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Micos
  • 87
  • 9
2

Another possible solution:

[list(map(int, x)) for x in lst]

Output:

[[1, 2, 3, 4, 5, 6, 7, 8, 9], 
[2, 3, 4, 5, 6, 7, 8, 9, 1], 
[3, 4, 5, 6, 7, 8, 9, 1, 2],
[4, 5, 6, 7, 8, 9, 1, 2, 3], 
[5, 6, 7, 8, 9, 1, 2, 3, 4], 
[7, 8, 9, 1, 2, 3, 4, 5, 6], 
[8, 9, 1, 2, 3, 4, 5, 6, 7], 
[9, 1, 2, 3, 4, 5, 6, 7, 8]]

EDIT

@Code-Apprentice claims that my solution does accomplish what the OP asked for, and that solution only works for the particular OP's example.

I have just used a random example to show that the results of my solution match the ones by @Micos and @GeorgeUdosen. Commenting @Micos's solution, the OP writes: "Thanks, this works".

lst = np.random.randint(123456789, 999999999, 8).astype(str).tolist()
print(lst)

# Original solution from PaulS
res1 = [list(map(int, x)) for x in lst] 

# Solution from Code-Apprentice
res2 = list(zip(*lst))

# Original solution from Micos
res3 = list(map(lambda x: list(map(lambda y: int(y), list(x))), lst))

finalLists = []
for splittingList in lst:
    workingList = []
    for character in splittingList:
       workingList.append(int(character))
    finalLists.append(workingList)
    
# solution from George Udosen
new_list = []
for i in lst[:2]:
    new_list.append([int(x) for x in i])

print('PaulS: ', res1), print('George Udosen: ', new_list), print('Micos: ', res3), print('Code-Apprentice', res2)
    
['305158777', '982760054', '851541517', '400278793', '657908393', '310483638', '794286097', '911226683']
PaulS:  [[3, 0, 5, 1, 5, 8, 7, 7, 7], [9, 8, 2, 7, 6, 0, 0, 5, 4], [8, 5, 1, 5, 4, 1, 5, 1, 7], [4, 0, 0, 2, 7, 8, 7, 9, 3], [6, 5, 7, 9, 0, 8, 3, 9, 3], [3, 1, 0, 4, 8, 3, 6, 3, 8], [7, 9, 4, 2, 8, 6, 0, 9, 7], [9, 1, 1, 2, 2, 6, 6, 8, 3]]
George Udosen:  [[3, 0, 5, 1, 5, 8, 7, 7, 7], [9, 8, 2, 7, 6, 0, 0, 5, 4]]
Micos:  [[3, 0, 5, 1, 5, 8, 7, 7, 7], [9, 8, 2, 7, 6, 0, 0, 5, 4], [8, 5, 1, 5, 4, 1, 5, 1, 7], [4, 0, 0, 2, 7, 8, 7, 9, 3], [6, 5, 7, 9, 0, 8, 3, 9, 3], [3, 1, 0, 4, 8, 3, 6, 3, 8], [7, 9, 4, 2, 8, 6, 0, 9, 7], [9, 1, 1, 2, 2, 6, 6, 8, 3]]
Code-Apprentice [('3', '9', '8', '4', '6', '3', '7', '9'), ('0', '8', '5', '0', '5', '1', '9', '1'), ('5', '2', '1', '0', '7', '0', '4', '1'), ('1', '7', '5', '2', '9', '4', '2', '2'), ('5', '6', '4', '7', '0', '8', '8', '2'), ('8', '0', '1', '8', '8', '3', '6', '6'), ('7', '0', '5', '7', '3', '6', '0', '6'), ('7', '5', '1', '9', '9', '3', '9', '8'), ('7', '4', '7', '3', '3', '8', '7', '3')]

Note that some of the other solutions do not give a transposed version of the lists.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
PaulS
  • 21,159
  • 2
  • 9
  • 26
0

If you like one liners.

a_list = ['123456789', '234567891', '345678912', '456789123', '567891234', '789123456', '891234567', '912345678']

new_list = [[int(character) for character in string] for string in a_list]

Output

[[1, 2, 3, 4, 5, 6, 7, 8, 9],
 [2, 3, 4, 5, 6, 7, 8, 9, 1],
 [3, 4, 5, 6, 7, 8, 9, 1, 2],
 [4, 5, 6, 7, 8, 9, 1, 2, 3],
 [5, 6, 7, 8, 9, 1, 2, 3, 4],
 [7, 8, 9, 1, 2, 3, 4, 5, 6],
 [8, 9, 1, 2, 3, 4, 5, 6, 7],
 [9, 1, 2, 3, 4, 5, 6, 7, 8]]
unltd_J
  • 476
  • 7
  • 14
  • This does not get the first two columns from the original list. They just happen to be the same as the first two rows of the example, so it is a coincidence that this gives the correct solution for that particular case. – Code-Apprentice Dec 11 '22 at 04:07
0

One quick way to get the columns as rows it to use zip():

my_list = [...]
transposed = zip(*my_list)

This gives an iterable that you can use in a for loop. If you just want the first two columns, then you can do

first_two_columns = list(transposed)[:2]

Note that this will give a list of tuples rather than a list of lists. For most cases this will be sufficient.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268