繁体   English   中英

如何打印仅包含字母的单词列表切片。 python? 但我错过了什么?

[英]How to print the slice of the list of words that only includes letters. python? But what am i missing?

这是我到目前为止所拥有的:

    strVar = 'Together We Thrive, Class of 2025!'


    def stringToList(string):
     listRes = list(string.split(" "))
     return listRes


    strVar = 'Together We Thrive, Class of 2025!'
    strVarList = print(stringToList(strVar))

我知道我需要 append 并在那里进行排序。

这些是说明:

  1. 使用 for 循环或内置 function 将提供的字符串转换为列表,并将结果存储在新变量中。
  2. 对列表进行排序。
  3. 打印列表中仅包含字母的部分。

我的答案应该是这样的:

    ['C', 'T', 'T', 'W', 'a', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'l', 'o', 'o', 'r', 'r', 's', 's', 't', 'v']

请尝试:

sorted([x for x in 'Together We Thrive, Class of 2025!' if x.isalpha()])

或者,传递已定义变量的名称。

这输出:

['C',
 'T',
 'T',
 'W',
 'a',
 'e',
 'e',
 'e',
 'e',
 'f',
 'g',
 'h',
 'h',
 'i',
 'l',
 'o',
 'o',
 'r',
 'r',
 's',
 's',
 't',
 'v']

请试试这个:

strvar = "Together we Thrive, Class of 2025!"

Listt=[ ]

for letter in strvar:
    if (letter.isalpha()):
        Listt.append(letter)

Listt.sort()

print(Listt)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM