I have list of list that looks like this:
listThing = [['apple','mango','cherry'],
['dog','cat','bird'],
['rose','jasmine','sunflower']
['hospital','house','school']
['chair','table','cupboard']
['book','pencil','pen']]
I want to write that list into files where the number of files is a predetermined value. Then, the number of lists in each file is the division of the number of all lists and number of files. So if:
number of file = 3
number of list in each file = number of all lists/number of file = 6/3 = 2
The outputs will look like this:
file1.txt
apple
mango
cherry
dog
cat
bird
file2.txt
rose
jasmine
sunflower
hospital
house
school
file3.txt
chair
table
cupboard
book
pencil
pen
This is what I've tried:
import math
allList = len(listThing)
numFile = 3
listInFile = math.ceil(allList/numFile)
for i in range(listInFile):
with open('file'+str(i)+'.txt', 'w') as out:
for n in range(listInFile):
# I don't know what should I do next
I do not know how to solve this problem. I'm hoping someone can help me to solve this problem. Thank you