Home > Net >  python store a split list into multiple files and items in same line
python store a split list into multiple files and items in same line

Time:05-23

I have a huge list with 9000 items. I already referred this post enter image description here

CodePudding user response:

No need for groupby, a simple loop with slicing is sufficient. You need to decide how to handle the extra items (add to the last list or add an extra file):

Mylist = [1234,45678,2314,65474,412,87986,21321,4324,68768,1133,712421,12132,898]
N = 3 # use 10 in your real life example

step = len(Mylist)//N
start = 0
for i, stop in enumerate(range(step, len(Mylist) step, step)):
    print(f'file{i}')
    print(Mylist[start:stop]) # save to file here instead
    start = stop

output:

file0
[1234, 45678, 2314, 65474]
file1
[412, 87986, 21321, 4324]
file2
[68768, 1133, 712421, 12132]
file3
[898]

Variant for adding to last file:

Mylist = [1234,45678,2314,65474,412,87986,21321,4324,68768,1133,712421,12132,898]
N = 3

step = len(Mylist)//N
start = 0

for i, stop in enumerate(range(step, len(Mylist), step)):
    print(f'file{i}')
    if i 1 == N:
        stop = len(Mylist)
    print(Mylist[start:stop]) # save to file here instead
    start = stop

output:

file0
[1234, 45678, 2314, 65474]
file1
[412, 87986, 21321, 4324]
file2
[68768, 1133, 712421, 12132, 898]
saving to file
Mylist = [1234,45678,2314,65474,412,87986,21321,4324,68768,1133,712421,12132,898]
N = 3

step = len(Mylist)//N
start = 0

for i, stop in enumerate(range(step, len(Mylist), step), start=1):
    if i == N:
        stop = len(Mylist)
    with open(f'file{i}.txt', 'w') as f:
        f.write(','.join(map(str,Mylist[start:stop])))
    start = stop
  • Related