2017-08-29 47 views
-3

我有這個名單,如何用一系列字母數字創建列表?

['1A11','1A18'] 

如何創建使用利用上述的範圍,並打印輸出蟒蛇列表:

['1A11','1A12','1A13','1A14','1A15','1A16','1A17','1A18'] 

感謝。

+2

使用列表理解和'對字符串.format'方法。你到底在嘗試什麼? –

回答

0

通過簡單的列表理解:

l = ['1A11','1A18'] 
result = [ l[0][:2]+str(i) for i in range(int(l[0][2:]), int(l[1][2:])+1) ] 

print(result) 

輸出:

['1A11', '1A12', '1A13', '1A14', '1A15', '1A16', '1A17', '1A18'] 
1
def make_range(pre, r): 
    return ['{0}{1}'.format(pre, i) for i in r] 

# Example 
print (make_range('1A1', range(1, 8))) 
# Will print 
>>> ['1A11','1A12','1A13','1A14','1A15','1A16','1A17','1A18'] 
相關問題