Python 从列表中的特定位置打印单词,每个单词都带有后缀

Python 从列表中的特定位置打印单词,每个单词都带有后缀,python,Python,我有一个列表,我想用python打印第四个位置后的所有单词,第三个位置后的每个单词都会后缀为.com 范例 my_list = ['apple', 'ball', 'cat', 'dog', 'egg', 'fish', 'rat'] 从上面开始,我想打印从“egg”开始的值,即:egg.com、fish.com、rat.com只需执行以下操作: 因为我在我的清单中[3:]: printi+“.com” 就是这样。代码 用法 请重新读取,阅读并提供。为我实现此功能对于此网站来说是离题的。你必须

我有一个列表,我想用python打印第四个位置后的所有单词,第三个位置后的每个单词都会后缀为.com

范例

my_list = ['apple', 'ball', 'cat', 'dog', 'egg', 'fish', 'rat']
从上面开始,我想打印从“egg”开始的值,即:egg.com、fish.com、rat.com

只需执行以下操作:

因为我在我的清单中[3:]: printi+“.com” 就是这样。

代码

用法


请重新读取,阅读并提供。为我实现此功能对于此网站来说是离题的。你必须做一个诚实的尝试,然后问一个关于你的算法或技术的具体问题。我的意思是,考虑到你的排名,这个问题很糟糕,但我还是会回答的。
def get_words(lst, word):
    ' Returns string of words starting from a particular word in list lst '
    # Use lst.index to find index of word in list
    #     slice (i.e. lst[lst.index(word):] for sublist of words from word in list
    #     list comprehension to add '.com' to each word starting at index
    #     join to concatenate words
    if word in lst:
        return ', '.join([x + '.com' for x in lst[lst.index(word):]])
    
my_list = ['apple', 'ball', 'cat', 'dog', 'egg', 'fish', 'rat']

print(get_words(my_list, 'egg'))  # egg.com, fish.com, rat.com
print(get_words(my_list, 'dog'))  # dog.com, egg.com, fish.com, rat.com
print(get_words(my_list, 'pig'))  # None