84669 Lernen von Personen
152542 Lernen von Personen
20005 Lernen von Personen
5487 Lernen von Personen
7821 Lernen von Personen
359900 Lernen von Personen
3350 Lernen von Personen
180660 Lernen von Personen
48569 Lernen von Personen
18603 Lernen von Personen
40936 Lernen von Personen
1549 Lernen von Personen
1183 Lernen von Personen
32909 Lernen von Personen
希望列表 c 所有元素首字母变大写,这样写为甚么会出错?该如何写?
c
c=['zz','yy','xx'] c[0:2]=c[0:2].capitalize() # 提示错误 AttributeError: 'list' object has no attribute 'capitalize'
光阴似箭催人老,日月如移越少年。
capitalize 是字串的方法,而 c[0:2] 是一個 list,所以你調用 captitalize 的時候會出錯.
capitalize
c[0:2]
captitalize
c = ['zz','yy','xx'] c = [string.capitalize() for string in c]
還有這樣也可以:
c = ['xx', 'yy', 'zz'] c = ' '.join(c).title().split()
P.S. 在使用 list 的時候,如果要操作的是整個串列,那不需要特別使用到切片,c[0:2] 在這裡是個不必要的做法.
給你參考!
@moling3650, 使用 title 真的是個有趣的主意,capitalize 只會將字串的首字大寫,而 title 則會將字串中所有的 單字 首字大寫。
title
見範例:
>>> string = 'my name is dokelung' >>> string.capitalize() 'My name is dokelung' >>> string.title() 'My Name Is Dokelung'
所以這樣也行:
>>> c = ['xx', 'yy', 'zz'] >>> ' '.join(c).title().split() ['Xx', 'Yy', 'Zz']
[_.capitalize() for _ in c]
写个不一样的
c = [_.title() for _ in c]
list对象没有capitalize方法
map(str.title, c[0:2])
capitalize
是字串的方法,而c[0:2]
是一個 list,所以你調用captitalize
的時候會出錯.還有這樣也可以:
P.S. 在使用 list 的時候,如果要操作的是整個串列,那不需要特別使用到切片,
c[0:2]
在這裡是個不必要的做法.給你參考!
@moling3650, 使用
title
真的是個有趣的主意,capitalize
只會將字串的首字大寫,而title
則會將字串中所有的 單字 首字大寫。見範例:
所以這樣也行:
写个不一样的
list对象没有capitalize方法
写个不一样的