Python 从两个列表创建自定义字符串
我制作了一个赠品模块,现在我正在尝试创建一个函数,使用两个类似的列表为所有获奖者返回一个祝贺字符串Python 从两个列表创建自定义字符串,python,python-3.x,string,Python,Python 3.x,String,我制作了一个赠品模块,现在我正在尝试创建一个函数,使用两个类似的列表为所有获奖者返回一个祝贺字符串 winners = ["john", "anna", "max", "george"] prizes = [(1, "apple"), (1, "pear"), (2, "carrots")] # (number of these prize avaible, the prize) 它们不需要和你看到的一样长,但是奖品中的数字总和总是和获奖者的长度一样 我理想的弦应该是这样的 "Congratu
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的sumx[0]为x
这是我到目前为止尝试过的,但当每个奖项有两个以上的获奖者时,它并没有真正的用处,而且我真的不知道如何知道何时在最后一次迭代中用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.
最好先在奖品列表上循环,然后将获奖者放在字符串结构中,例如: winners=[约翰、安娜、马克斯、乔治] 奖品=[2,花生,2,胡萝卜] def祝贺获奖者、奖品: s=祝贺您 i=0 奖品中的p: w=','。[i:i+p[0]] s+={}表示赢得{}{},.formatw,1,p[1],如果p[0]==1 i+=p[0] 返回s 祝贺获奖者、奖品 祝贺约翰、安娜每人赢得1枚花生,祝贺马克斯、乔治每人赢得1枚胡萝卜, 您需要修改w Phase,以备不时之需,而不是,例如: w=','。[i:i+p[0]] w='and'.joinw.rsplit','1 它将替换最后出现的“,”by“and”,最后一个字符串如下所示
祝贺john和anna各赢了一个花生,max和george各赢了一个胡萝卜,最好先在奖品列表上循环,然后将获奖者放在字符串结构中,例如: winners=[约翰、安娜、马克斯、乔治] 奖品=[2,花生,2,胡萝卜] def祝贺获奖者、奖品: s=祝贺您 i=0 奖品中的p: w=','。[i:i+p[0]] s+={}表示赢得{}{},.formatw,1,p[1],如果p[0]==1 i+=p[0] 返回s 祝贺获奖者、奖品 祝贺约翰、安娜每人赢得1枚花生,祝贺马克斯、乔治每人赢得1枚胡萝卜, 您需要修改w Phase,以备不时之需,而不是,例如: w=','。[i:i+p[0]] w='and'.joinw.rsplit','1 它将替换最后出现的“,”by“and”,最后一个字符串如下所示 祝贺约翰和安娜每人赢得1枚花生,祝贺马克斯和乔治每人赢得1枚胡萝卜,请尝试以下方法:
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]):
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]
print(st)
请试试这个:
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]):
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]
print(st)
问题陈述假设>奖品中的数字之和始终与获奖者的长度相同问题陈述假设>奖品中的数字之和始终与获奖者的长度相同