很抱歉打扰一个简单的问题,但我无法提出解决方案。 到目前为止,我已经完成了以下解决方案。我正在尝试从“ 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])
答案 0 :(得分:0)
使用列表理解:
print([x[1] for x in "The weather is very nice today I feel warmer!".split() if len(x)>1])
答案 1 :(得分:0)
如果我理解问题,您将把一个字符串转换为它的字符列表。这是您可以使用的示例代码:
line = "The weather is very nice today I feel warmer!"
chars = []
chars.extend(line)
print(chars)
答案 2 :(得分:0)
本着使代码与代码尽可能接近的精神,这是一个简单的解决方案。有关更多详细信息,请参见嵌入式注释。顺便说一句-删除了不必要的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
output_ls:
['h', 'e', 's', 'e', 'i', 'o', 'e', 'a']