【发布时间】:2021-09-05 01:16:51
【问题描述】:
好的,我已经包含了我写的代码,当它打印我得到的列表时:你给香蕉,巧克力对两次(一次是香蕉,巧克力,一次是巧克力,香蕉)。我确实找到了没有这个问题的代码,但我的问题是我不明白它是如何解决违背目的的问题。
我的代码:
FLAVORS = [
"Banana",
"Chocolate",
"Lemon",
"Pistachio",
"Raspberry",
"Strawberry",
"Vanilla",
]
for x in FLAVORS:
for y in FLAVORS:
if x != y:
print(x + ", " + y)
有效但我不明白的代码:
FLAVORS = [
"Banana",
"Chocolate",
"Lemon",
"Pistachio",
"Raspberry",
"Strawberry",
"Vanilla",
]
for flavor_one in FLAVORS:
for flavor_two in range(FLAVORS.index(flavor_one)+1,len(FLAVORS)):
print(flavor_one + ", " + FLAVORS[flavor_two])
【问题讨论】:
标签: python duplicates combinations