从两个列表创建自定义字符串

2024-09-30 18:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个赠品模块,现在我尝试创建一个函数,用两个类似的列表为所有获奖者返回一个祝贺字符串。你知道吗

winners = ["john", "anna", "max", "george"]
prizes = [(1, "apple"), (1, "pear"), (2, "carrots")]
# (number of these prize avaible, the prize)

他们不需要和你看到的一样长,但是奖品中数字的总和总是和获奖者的长度一样。你知道吗

我理想的弦应该是这样的。你知道吗

"Congratulations to john for winning 1 apple, anna for winning 1 pear, and max and george for winning 1 carrot each."

我知道我可能会扩展奖品列表并在以后压缩这两个奖品但是我确实在尝试将有多个获奖者的奖品分组在一起。因为这个列表最多可以有100个奖品和总和(奖品中x的x[0])。你知道吗

这是我迄今为止尝试过的,但当每个奖有两个以上的获奖者时,它并没有真正的用处,而且我真的不知道如何知道在最后一次迭代中用and替换a。你知道吗

winners = ["john", "anna", "max", "george"]
prizes = [(2, "peanuts"), (2, "carrot")]


def congratulations(winners, prizes):
    s = "Congratulations to "
    for w in winners:
        for p in prizes:
            s += "{} for winning {} {}, ".format(w, p[0], p[1])
            break
    return s


print(congratulations(winners, prizes))
#Congratulations to john for winning 2 peanuts, anna for winning 2 peanuts, max for winning 2 peanuts, george for winning 2 peanuts,

不过,有些事情我并不在乎,比如奖品是用复数形式写的,或者你需要在最后加一个。谢谢你的帮助。你知道吗

多亏了亨利,我做了一些小的修正,但代码是一样的。你知道吗

winners = ["john", "anna", "max", "george", "carlos"]
prizes = [(1, "apple"), (3, "pears"), (1, "carrots")]


def congratulations(winners, prizes):
    s = "Congratulations to "
    i = 0
    for p in prizes:
        w = ", ".join(winners[i : i + p[0]])
        w = " and ".join(w.rsplit(", ", 1))
        s += "{} for winning {} {}{}; ".format(w, 1, p[1], "" if p[0] == 1 else " each")
        i += p[0]
    s = s[:-2] + "."
    k = s.rfind(";")
    s = s[:k] + "; and" + s[k + 1 :]
    return s


w = congratulations(winners, prizes)
print(w)
# Congratulations to john for winning 1 apple, anna, max and george for winning 1 pears each, and carlos for winning 1 carrots.

Tags: andtoappleforjohnmaxannageorge
2条回答

请试试这个:

    winners = ["john", "anna", "max", "george"]
    prizes = [(2, "peanuts"), (2, "carrot")]

    s = "Congratulations to "
    winners_index = 0
    st = ""
    for p in prizes:
        i = 0
        for w_in in range(winners_index,winners_index+p[0]):
            print(w_in,i)
            if i == 0:
                st = st + s +winners[w_in]+','
            elif i == p[0]-1:
                st = st + winners[w_in]
            else:
                st = st + winners[w_in]+','
            i = i + 1
        st= st+ ' for winning '+p[1]+','
        winners_index = winners_index + p[0]

最好先循环查看奖品列表,然后将获奖者放在字符串结构中,例如:

winners = ["john", "anna", "max", "george"]
prizes = [(2, "peanuts"), (2, "carrot")]

def congratulations(winners, prizes):
    s = "Congratulations to "
    i = 0
    for p in prizes:
        w = ', '.join(winners[i: i + p[0]])
        s += "{} for winning {} {}{}, ".format(w, 1, p[1], '' if p[0]==1 else " each")
        i += p[0]

    return s


print(congratulations(winners, prizes))
# Congratulations to john, anna for winning 1 peanuts each, max, george for winning 1 carrot each, 

如果首选and而不是,,则需要修改wpharse,例如:

w = ', '.join(winners[i: i + p[0]])
w = ' and '.join(w.rsplit(', ', 1))

它将', '的最后一次出现替换为' and ',最后的字符串如下所示 Congratulations to john and anna for winning 1 peanuts each, max and george for winning 1 carrot each,

相关问题 更多 >