很抱歉打扰一个简单的问题,但我无法想出解决方案。到目前为止,我已经完成了下面的解决方案。我试图从“koc”中提取次要字符并将它们列在一个列表中。下面的解决方案只打印出次要字符。我想从这些字符中创建一个列表。谢谢你
koc = "The weather is very nice today I feel warmer!"
sent = koc.title()
for kc in sent.split():
if len(kc) == 1:
continue
else:
print(kc[1])
本着让代码尽可能接近您的代码的精神,这里有一个简单的解决方案。有关更多详细信息,请参阅内嵌注释。顺便说一句 - 删除了不必要的 else 语句以防止嵌套:)
koc = "The weather is very nice today I feel warmer!"
sent = koc.title()
output_ls = [] # initiate list here
for kc in sent.split():
if len(kc) == 1:
continue
output_ls.append(kc[1]) # add to the end of the list
输出_ls:
['h', 'e', 's', 'e', 'i', 'o', 'e', 'a']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句