2017年9月7日 星期四

Python - Building a list using for loop and other work about list - list用迴圈產生、list串接(相加)、使用迴圈切割list

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

System version :Windows 10
list1 = ['a'+str(x) for x in range(7)]
print(list1)
print('='*40)

list2=[]
for a in range(2):
    list2=list2+list1
print(list2)
print('='*40)

num = 3
for a in range(0,len(list2),num):
    print(list2[a:a+num])
執行結果:
['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']
========================================
['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6']
========================================
['a0', 'a1', 'a2']
['a3', 'a4', 'a5']
['a6', 'a0', 'a1']
['a2', 'a3', 'a4']
['a5', 'a6']

沒有留言:

張貼留言