2013-08-31 100 views

回答

1

只需在每个字符串上拨打capitalize即可。请注意,小写字母,其余

l = ['This', 'is', 'a', 'list'] 
print [x.capitalize() for x in l] 
['This', 'Is', 'A', 'List'] 

如果您需要在其他字母保留的情况下,做到这一点,而不是

l = ['This', 'is', 'a', 'list', 'BOMBAST'] 
print [x[0].upper() + x[1:] for x in l] 
['This', 'Is', 'A', 'List', 'BOMBAST'] 
0
x=['a', 'test','string'] 

print [a.title() for a in x] 

['A', 'Test', 'String']

由于regex被标记过,你可以使用类似以下的东西

>>> import re 
>>> x=['a', 'test','string'] 
>>> def repl_func(m): 
     return m.group(1) + m.group(2).upper() 


>>> [re.sub("(^|\s)(\S)", repl_func, a) for a in x] 
['A', 'Test', 'String'] 
相关问题