我怎样才能把这个函数变成一个循环? Python

How can I make this function into a loop? Python

我想再次调用该函数以 return 一个包含 sampled_list 的 5 个结果的新列表。谢谢大家

import random
emoji_list  = ['', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

sampled_list=random.sample(emoji_list, k=5)

def listToString(sampled_list):
    # initialize an empty string
    str1 = ""
    # traverse in the string
    for i in sampled_list:
        str1 += i
        # return string
    return str1

可能你想要这样的东西:

Try it online!

import random

def emojiString():
    emoji_list  = ['', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    return ''.join(random.sample(emoji_list, k = 5))

print(emojiString())
print(emojiString())

输出:




如果您想将多个结果累积到列表中,请执行以下操作:

Try it online!

import random

def emojiString():
    emoji_list  = ['', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '','', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    return ''.join(random.sample(emoji_list, k = 5))

def emojiAddToList(l):
    l.append(emojiString())

l = []
emojiAddToList(l)
emojiAddToList(l)
emojiAddToList(l)
print(l)

输出:

['', '', '']