Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何提取某些单词的第一个字母_Python_List_List Comprehension - Fatal编程技术网

Python 如何提取某些单词的第一个字母

Python 如何提取某些单词的第一个字母,python,list,list-comprehension,Python,List,List Comprehension,我有一个作业,要求我给输入字符串“昵称”。到目前为止,我能够提取字符串中每个单词的第一个字母,但我需要排除代词和小于三个字符的单词 这就是我到目前为止所做的: def nickname(): name = input('Would you like to nickname a sentence or would you like to quit?: ') if name == "quit": print("bye")

我有一个作业,要求我给输入字符串“昵称”。到目前为止,我能够提取字符串中每个单词的第一个字母,但我需要排除代词和小于三个字符的单词

这就是我到目前为止所做的:

def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word[0] for word in words]
    return(" ".join(letters).upper())
试一试

试一试


代词={'i',me',you',…}
,然后使用条件列表理解:
如果len(word)>2和word.lower()不在代词中,则使用条件列表理解:
@Alexander如果要显示完整的解决方案,请发布答案。
代词={'i',me',you…}
,然后使用条件列表理解:
[word[0]对于words中的word]如果len(word)>2和word.lower()不在代词中]
@Alexander如果你想展示完整的解决方案,就发布一个答案。
word[0]
-主要是第一个字符,但是你需要检查
len(word)>3
并获得
word
他只想要每个符合条件的单词的第一个字符。
word[0]
-主要是第一个字符,但您需要检查
len(word)>3
并获取
word
,他只需要符合条件的每个单词的第一个字符。
def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word for word in words if len(word)>3]
    return(" ".join(letters).upper())
def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?:   ') 
    if name == "quit":
        print("bye")
    else:
      words = name.split()
      letters = [word[0] for word in words if len(word)>3]
      return("".join(letters).upper())

print(nickname())