0

很抱歉打扰一个简单的问题,但我无法提出解决方案。到目前为止,我已经完成了以下解决方案。我正在尝试从“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])
4

3 回答 3

0

使用列表推导:

print([x[1] for x in  "The weather is very nice today I feel warmer!".split() if len(x)>1])
于 2018-11-04T22:47:00.953 回答
0

如果我理解问题,您会将字符串转换为其字符列表。这是您可以使用的示例代码:

line = "The weather is very nice today I feel warmer!"
chars = []
chars.extend(line)
print(chars) 
于 2018-11-04T22:49:05.593 回答
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

输出_ls:

['h', 'e', 's', 'e', 'i', 'o', 'e', 'a']
于 2018-11-04T22:57:01.500 回答