Home > Software design >  how do i convert results to list python
how do i convert results to list python

Time:06-23

I have this code I'm trying to run by using two columns of a csv file that I've converted into lists and used those lists to get a < and > comparison between the numbers inside, now i want to get the results from this comparison in a list format of multiple lists that I want to display in an interval of six digits(the results) per list

eg I get

1 2 3 4 5 6 7 8 9 10 11 12

and i want to display this as

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

this is the code I'm using for comparing the lists

'''
    for i in range(len(fsa)):
        if fsa[i] < ghf[i]:
            print('1')
        else:
            print('0') 
'''

the code that's not working which is the one for showing results in an intervalled list format is this one

'''
print()
start = 0
end = len(''' i want the length of my results from the previous code, the 1's and 0's here. ''')
for x in range(start,end,6):
    print('''i want the results here as my list'''[x:x 6])
'''

I'm a beginner, please help, how do i make the results a list?

CodePudding user response:

you just need to make a new list and append it instead of print. your code is working as per the requirements you have stated

...
...
temp = []
for x in range(start,end,6):
    temp.append(fsa[x:x 6])
    
print(temp)
#[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]

CodePudding user response:

i got the answer i wanted. Incase someone else was suffering with this as well here's my solution

'''
kol = []
for i in range(len(fsa)):
    if fsa[i] < ghf[i]:
        kol.append('1')
    else:
        kol.append('0')


start = 0
end = len(fsa)
for x in range(start,end,6):
    print(kol[x:x 6])
'''

outcome

'''
    ['1', '1', '0', '0', '1', '1']
    ['1', '0', '0', '1', '0', '0']
    ['0', '0', '0', '0', '1', '1']
    ['1', '1', '1', '0', '1', '1']
'''
  • Related