針對很普遍的每個元素的操作會遍歷每個元素進行操作。
這里給出了幾種寫法,列表每個元素自增等數學操作同理;
示例:整形列表ilist加1個數、元素類型轉字符串:
1 ilist = [1, 2, 3, 10, 11, 12] 2 3 4 # 每個元素加5,四種方法 5 for i, v in enumerate(ilist): ilist[i] = v + 5 6 [ x+5 for x in ilist ] 7 map(lambda x:x+5, ilist) # 僅python 2 8 list(map(lambda x:x+5, ilist)) 9 [*map(lambda x:x+5, ilist)] # 僅python 3 10 11 12 # 整形元素轉字符串,兩種方法 13 list(map(str, ilist)) 14 [*map(str, ilist)] # 僅python 3
執行結果如下圖:
參考:
https://www.geeksforgeeks.org/python-convert-a-list-of-multiple-integers-into-a-single-integer/
https://stackoverflow.com/questions/3371269/call-int-function-on-every-list-element