[英]How to increment a list in python containing both numbers and characters?
我有一個[1,2,3,a,b,M]列表,需要將每個變量加1。
我已經嘗試過使用我可以用作的號碼:
a = [1,2,3]
for i in range(len(a)):
a[i] += 1
print a
對於字符我也可以使用:
ch = 'M'
x = chr(ord(ch) + 1)
print ("The incremented character value is : ")
print (x)
但是我在一起卻無法參加。 無論如何,我可以俱樂部嗎?
您可以嘗試列表理解
[e+1 if isinstance(e,int) else chr(ord(e) + 1) for e in [1,2,3,'a','b','M']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.