我的列表如下所示:
listThing = [['apple','mango','cherry'],
['dog','cat','bird'],
['rose','jasmine','sunflower']
['hospital','house','school']
['chair','table','cupboard']
['book','pencil','pen']]
我想把这个列表写到文件中,其中文件的数量是一个预定的值。然后,每个文件中的列表数就是所有列表数和文件数的除法。所以如果:
number of file = 3
number of list in each file = number of all lists/number of file = 6/3 = 2
输出如下所示:
文件1.txt
apple
mango
cherry
dog
cat
bird
文件2.txt
rose
jasmine
sunflower
hospital
house
school
文件3.txt
chair
table
cupboard
book
pencil
pen
这就是我尝试过的:
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
我不知道如何解决这个问题。我希望有人能帮我解决这个问题。谢谢
试试这个:
相关问题 更多 >
编程相关推荐