How to remove items from list and make items join - python

I have a list which has numbers, but because its appended to a list using for loop it has '\n' and I don't know how to remove it.
the list looks like this
['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
I want to remove ' ' and '\n' so it would look like this
[374559227,437159430,372410275,784516257,280661123,935683871,675547486]

Join to a string and split the newlines:
l = [
'3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5',
'9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n',
'7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6',
'1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n',
'6', '7', '5', '5', '4', '7', '4', '8', '6'
]
print([int(x) for x in ''.join(l).split('\n')])
>>> [374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]

You can use itertools.groupby:
>>> from itertools import groupby
>>> lst = ['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
>>> [int(''.join(digits)) for is_number, digits in groupby(lst, lambda x: x != '\n') if is_number]
[374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]

You can use reduce function
from functools import reduce
lst = ['3', '7', '4', '5', '5', '9', '2', '2', '7', '\n', '4', '3', '7', '1', '5', '9', '4', '3', '0', '\n', '3', '7', '2', '4', '1', '0', '2', '7', '5', '\n', '7', '8', '4', '5', '1', '6', '2', '5', '7', '\n', '2', '8', '0', '6', '6', '1', '1', '2', '3', '\n', '9', '3', '5', '6', '8', '3', '8', '7', '1', '\n', '6', '7', '5', '5', '4', '7', '4', '8', '6']
lst_result = [int(n) for n in reduce(lambda x, y: f"{x}{y}", lst).split('\n')]
Output:
[374559227, 437159430, 372410275, 784516257, 280661123, 935683871, 675547486]

Related

How do I split this list into another list within a list?

I want to split the following list
['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']
Into another list called userIDs where the 18 first characters (the IDs) can be put into a list with the smaller numbers until the | symbol.
Example:
[[198093765087330304,00456430],[(next ID),(next smaller number)]]
You can use a nested list comprehension:
>>> [part.split(":") for part in "".join(my_list).split("|")]
[['198093765087330304', '00456430'], ['811309194182656011', '004560'],
['867056035445735445', '00456610'], ['817147352618893401', '0045630'],
['409292709342478358', '00456490'], ['315079592966619147', '0045630'],
['563816877806059520', '00456630'], ['237251807405932564', '00456450'],
['172002275412279296', '0045620'], ['302050872383242240', '00456310'],
['941402843221487706', '00456390'], ['460023247203991552', '0045620'],
['423536606704828416', '00456470']]
My solution puts your list into one string and then splits it twice, first with "|" and then with ":"
my_list = ['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']
s = '' # define string
for element in my_list: # for each element in list add it to a string
s += element
s = s.split(sep="|") # split the string with '|' separator
for i in range(len(s)):
s[i] = s[i].split(sep=':') # split each element of list with ':' separator
print(list(s))
Result: [['198093765087330304', '00456430'], ['811309194182656011', '004560'], ['867056035445735445', '00456610'], ['817147352618893401', '0045630'], [...
q = ['1', '9', '8', '0', '9', '3', '7', '6', '5', '0', '8', '7', '3', '3', '0', '3', '0', '4', ':', '0', '0', '4', '5', '6', '4', '3', '0', '|', '8', '1', '1', '3', '0', '9', '1', '9', '4', '1', '8', '2', '6', '5', '6', '0', '1', '1', ':', '0', '0', '4', '5', '6', '0', '|', '8', '6', '7', '0', '5', '6', '0', '3', '5', '4', '4', '5', '7', '3', '5', '4', '4', '5', ':', '0', '0', '4', '5', '6', '6', '1', '0', '|', '8', '1', '7', '1', '4', '7', '3', '5', '2', '6', '1', '8', '8', '9', '3', '4', '0', '1', ':', '0', '0', '4', '5', '6', '3', '0', '|', '4', '0', '9', '2', '9', '2', '7', '0', '9', '3', '4', '2', '4', '7', '8', '3', '5', '8', ':', '0', '0', '4', '5', '6', '4', '9', '0', '|', '3', '1', '5', '0', '7', '9', '5', '9', '2', '9', '6', '6', '6', '1', '9', '1', '4', '7', ':', '0', '0', '4', '5', '6', '3', '0', '|', '5', '6', '3', '8', '1', '6', '8', '7', '7', '8', '0', '6', '0', '5', '9', '5', '2', '0', ':', '0', '0', '4', '5', '6', '6', '3', '0', '|', '2', '3', '7', '2', '5', '1', '8', '0', '7', '4', '0', '5', '9', '3', '2', '5', '6', '4', ':', '0', '0', '4', '5', '6', '4', '5', '0', '|', '1', '7', '2', '0', '0', '2', '2', '7', '5', '4', '1', '2', '2', '7', '9', '2', '9', '6', ':', '0', '0', '4', '5', '6', '2', '0', '|', '3', '0', '2', '0', '5', '0', '8', '7', '2', '3', '8', '3', '2', '4', '2', '2', '4', '0', ':', '0', '0', '4', '5', '6', '3', '1', '0', '|', '9', '4', '1', '4', '0', '2', '8', '4', '3', '2', '2', '1', '4', '8', '7', '7', '0', '6', ':', '0', '0', '4', '5', '6', '3', '9', '0', '|', '4', '6', '0', '0', '2', '3', '2', '4', '7', '2', '0', '3', '9', '9', '1', '5', '5', '2', ':', '0', '0', '4', '5', '6', '2', '0', '|', '4', '2', '3', '5', '3', '6', '6', '0', '6', '7', '0', '4', '8', '2', '8', '4', '1', '6', ':', '0', '0', '4', '5', '6', '4', '7', '0']
you can do this series of operations:
First, you convert everything into a string, replace colons with commas and then split at |. Then pack each individual entry into a separate list.
[[a] for a in ''.join(q).replace(':', ',').split('|')]
Edit:
finding first entry only:
[[int(a.split(':')[0])] for a in ''.join(q).split('|')]

Python Bubble Sort, 10 being considered < 5, 7 etc [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I have created a bubble sort program in which the user is able to enter the values which will be stored in the array which will be sorted by the bubble sort algorithm, however the output is not what I expect if i enter the values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 will be sorted to just after 1 changing the sorted list to 1, 10, 2, 3, 4, 5, 6, 7, 8, 9 which makes me believe that 10 is being read as 1 instead of 10 but I have no idea why.
def initlist():
unsortedlist=[]
for counter in range (0,10):
print("Please enter value", (counter+1))
temp = input("")
unsortedlist.append(temp)
return unsortedlist
def BubbleSort(unsortedlist):
passes = 0
print ("Fixed Loop")
print ("Initial State:", unsortedlist)
passes = 0
swaps = 0
for outer in range(len(unsortedlist) -1, 0, -1):
passes += 1
for inner in range (outer):
if unsortedlist[inner] > unsortedlist[inner + 1]:
swaps +=1
temp = unsortedlist[inner]
unsortedlist[inner] = unsortedlist[inner+1]
unsortedlist[inner + 1] = temp
print("Pass", passes, ":", unsortedlist)
input("Press Any Key To Continue")
Output
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 1 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 2 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 3 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 4 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 5 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 6 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 7 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 8 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
Pass 9 : ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']
temp = input("") reads input as strings. you are doing a lexicographical sorting here.
If you want to sort integers, then change to temp = int(input(""))

Grouping Lists into specific groups

I'm wondering if it is possible to convert the listings into a specific groups to which I could place them in a table format later on.
This is the output that I needed to group, I converted them into a list so that I could easily divide them in table manner.
f=open("sample1.txt", "r")
f.read()
Here's the output:
'0245984300999992018010100004+14650+121050FM-12+004699999V0203001N00101090001CN008000199+02141+01971101171ADDAY141021AY241021GA1021+006001081GA2061+090001021GE19MSL +99999+99999GF106991021999006001999999KA1120N+02111MD1210141+9999MW1051REMSYN10498430 31558 63001 10214 20197 40117 52014 70544 82108 333 20211 55062 56999 59012 82820 86280 555 60973=\n'
Here's what I have done already. I have managed to change it into a list which resulted in this output:
with open('sample1.txt', 'r') as file:
data = file.read().replace('\n', '')
print (list(data))
The Output:
['0', '2', '4', '5', '9', '8', '4', '3', '0', '0', '9', '9', '9', '9', '9', '2', '0', '1', '8', '0', '1', '0', '1', '0', '0', '0', '0', '4', '+', '1', '4', '6', '5', '0', '+', '1', '2', '1', '0', '5', '0', 'F', 'M', '-', '1', '2', '+', '0', '0', '4', '6', '9', '9', '9', '9', '9', 'V', '0', '2', '0', '3', '0', '0', '1', 'N', '0', '0', '1', '0', '1', '0', '9', '0', '0', '0', '1', 'C', 'N', '0', '0', '8', '0', '0', '0', '1', '9', '9', '+', '0', '2', '1', '4', '1', '+', '0', '1', '9', '7', '1', '1', '0', '1', '1', '7', '1', 'A', 'D', 'D', 'A', 'Y', '1', '4', '1', '0', '2', '1', 'A', 'Y', '2', '4', '1', '0', '2', '1', 'G', 'A', '1', '0', '2', '1', '+', '0', '0', '6', '0', '0', '1', '0', '8', '1', 'G', 'A', '2', '0', '6', '1', '+', '0', '9', '0', '0', '0', '1', '0', '2', '1', 'G', 'E', '1', '9', 'M', 'S', 'L', ' ', ' ', ' ', '+', '9', '9', '9', '9', '9', '+', '9', '9', '9', '9', '9', 'G', 'F', '1', '0', '6', '9', '9', '1', '0', '2', '1', '9', '9', '9', '0', '0', '6', '0', '0', '1', '9', '9', '9', '9', '9', '9', 'K', 'A', '1', '1', '2', '0', 'N', '+', '0', '2', '1', '1', '1', 'M', 'D', '1', '2', '1', '0', '1', '4', '1', '+', '9', '9', '9', '9', 'M', 'W', '1', '0', '5', '1', 'R', 'E', 'M', 'S', 'Y', 'N', '1', '0', '4', '9', '8', '4', '3', '0', ' ', '3', '1', '5', '5', '8', ' ', '6', '3', '0', '0', '1', ' ', '1', '0', '2', '1', '4', ' ', '2', '0', '1', '9', '7', ' ', '4', '0', '1', '1', '7', ' ', '5', '2', '0', '1', '4', ' ', '7', '0', '5', '4', '4', ' ', '8', '2', '1', '0', '8', ' ', '3', '3', '3', ' ', '2', '0', '2', '1', '1', ' ', '5', '5', '0', '6', '2', ' ', '5', '6', '9', '9', '9', ' ', '5', '9', '0', '1', '2', ' ', '8', '2', '8', '2', '0', ' ', '8', '6', '2', '8', '0', ' ', '5', '5', '5', ' ', '6', '0', '9', '7', '3', '=']
My goal is to group them into something like these:
0245,984300,99999,2018,01,01,0000,4,+1....
The number of digits belonging to each column is predetermined, for example there are always 4 digits for the first column and 6 for the second, and so on.
I was thinking of concatenating them. But I'm not sure if it would be possible.
You can use operator.itemgetter
from operator import itemgetter
g = itemgetter(slice(0, 4), slice(4, 10))
with open('sample1.txt') as file:
for line in file:
print(g(line))
Or even better you can make the slices dynamically using zip and itertools.accumulate:
indexes = [4, 6, ...]
g = itemgetter(*map(slice, *map(accumulate, zip([0]+indexes, indexes))))
Then proceed as before
I would recommend naming everything if you actually want to use this data, and double checking that all the lengths make sense. So to start you do
with open('sample1.txt', 'r') as file:
data = file.read().rstrip('\n"')
first, second, *rest = data.split()
if len(first) != 163:
raise ValueError(f"The first part should be 163 characters long, but it's {len(first)}")
if len(second) != 163:
raise ValueError(f"The second part should be characters long, but it's {len(first)}")
So now you have 3 variables
first is "0245984300999992018010100004+14650+121050FM-12+004699999V0203001N00101090001CN008000199+02141+01971101171ADDAY141021AY241021GA1021+006001081GA2061+090001021GE19MSL"
second is "+99999+99999GF106991021999006001999999KA1120N+02111MD1210141+9999MW1051REMSYN10498430"
rest is ['31558', '63001', '10214', '20197', '40117', '52014', '70544', '82108', '333', '20211', '55062', '56999', '59012', '82820', '86280', '555', '60973']
And then repeat that idea
date, whatever, whatever2, whatever3 = first.split('+')
and then for parsing the first part I would just have a list like
something = date[0:4]
something_else = date[4:10]
third_thing = date[10:15]
year = [15:19]
month = [19:21]
day = [21:23]
and so on. And then you can use all these variables in the code that analyzes them.
If this is some sort of standard, you should look for a library that parses strings like that or write one yourself.
Obviously name the variables better

How to add a list to another list

I have a function called "organizer" which takes 2 values a list and an index(number), to reorganize this list, after that it return a new list.
I want to execute this function 10 times to create 10 lists and add this lists to a single list of lists for later use, the program acomplish this, however it repeat the last list 10 times, it does not add the other lists.
I put the function "organizer" in here,because Im unable to find where the problem relies so more information may be needed.
I have been using the print function along the function to see where it fails, and it creates the lists as I desire, the problem is that after creating them it just copy the last created list as many times as the loop goes on. It takes the final list produced and copy it several times
Here is the code:
number_list = ["12","11","10","9","8","7","6","5","4","3","2","1"]
indexes = [0,5,6,8,10,4,5,2,1,9]
def organizer(list,index): # Takes a list and reorganize it by a number which is an index.
number1 = index - 1
count_to_add = -1
list_to_add = []
for item in range(number1):
count_to_add += 1
a = list[count_to_add]
list_to_add.append(a)
number2 = index - 1
for item1 in range(number2):
del list[0]
list.extend(list_to_add)
return list
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
count = -1
for item in list_indexes:
count += 1
result = organizer(number_list, list_indexes[count])
final_list.append(result)
return final_list
final_output = lists_creator(indexes)
print(final_output)
Here is the result:(the same list 10 times)
[['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8'], ['7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']]
Note: If I change the line 28 in list_creator function
final_list.append(result)
for
final_list.extend(result)
the result is:
['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8', '7', '6', '5', '4', '7', '6', '5', '4', '3', '2', '1', '12', '11', '10', '9', '8']
Which is the result that I desire but is not a group of lists.
you are using the same list every time, so the result consists of actually one list,
in your list_creator method, you have to give to organizer method a copy of your original list number_list, otherwise you just mutate over and over again the same one list:
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
count = -1
for item in list_indexes:
count += 1
result = organizer(list(number_list), list_indexes[count])
final_list.append(result)
return final_list
def lists_creator(list_indexes): # Create a list of 10 lists ,with 12 elements each.
final_list = []
list = []
for i in range(len(list_indexes)):
result = organizer(number_list, list_indexes[i])
list.extend(result)
n = 0
m = 12
for n_times in range(len(list_indexes)):
final_list.append(list[n:m])
n = m
m += 12
return final_list

conver a string of lists to a list of lists of lists [duplicate]

This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 7 years ago.
I have this string:
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
and I want to return/output:
[['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']]
how do i get to this list of lists from that huge string?
ast.literal_eval is a good for exactly that.
>>> num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
>>> import ast
>>> list(ast.literal_eval(num))
[['1', '9', '7', '6'], ['2', '0', '8', '3', '7'], ['3', '8', '5', '7', '9', '10', '4']]
You can use AST:
import ast
num="['1', '9', '7', '6'],['2', '0', '8', '3', '7'],['3', '8', '5', '7', '9', '10', '4']"
num = list(ast.literal_eval(num))

Categories