Combining 2 lists in python

前端 未结 2 993
夕颜
夕颜 2020-12-02 02:40

I have 2 lists each of equal size and am interested to combine these two lists and write it into a file.

alist=[1,2,3,5] 
blist=[2,3,4,5] 

2条回答
  •  醉酒成梦
    2020-12-02 02:50

    # combine the lists
    zipped = zip(alist, blist)
    
    # write to a file (in append mode)
    file = open("filename", 'a') 
    for item in zipped:
        file.write("%d, %d\n" % item) 
    file.close()
    

    The resulting output in the file will be:

     1,2
     2,3
     3,4
     5,5
    

提交回复
热议问题