Python3:如何使用While循环从列表中删除重复项
这是我目前的代码:Python3:如何使用While循环从列表中删除重复项,python,python-3.x,list,while-loop,Python,Python 3.x,List,While Loop,这是我目前的代码: sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami '] finished_sandwiches = [] print("Ordered sandwiches: " , sandwich_orders) while sandwich_orders: complete_sandwiches = sandwich_orders.pop()
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
finished_sandwiches = []
print("Ordered sandwiches: " , sandwich_orders)
while sandwich_orders:
complete_sandwiches = sandwich_orders.pop()
print("Now making your " + complete_sandwiches.title() + "sandwich")
finished_sandwiches.append(complete_sandwiches.title())
for finished_sandwich in finished_sandwiches:
print("Your " + finished_sandwich.title() + " sandwich is now ready.")
这是我运行程序时的当前程序输出:
Ordered sandwiches: ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
Now making your Pastrami sandwich
Now making your Knuckle sandwich
Now making your Pastrami sandwich
Now making your Blt sandwich
Now making your Ham sandwich
Now making your Pastrami sandwich
Now making your Pbj sandwich
Your Pastrami sandwich is now ready.
Your Knuckle sandwich is now ready.
Your Pastrami sandwich is now ready.
Your Blt sandwich is now ready.
Your Ham sandwich is now ready.
Your Pastrami sandwich is now ready.
Your Pbj sandwich is now ready.
我正在尝试使用While循环从列表中删除“Pastrami”的所有实例,但我不太确定如何格式化它。我试着把这个代码放到上面的while循环中
sandwiches = [sandwich_orders , finished_sandwiches , complete_sandwiches]
if 'pastrami' in sandwiches:
sandwiches.remove('pastrami')
虽然代码运行时没有任何错误,但什么也没有发生。我真的希望输出结果如下:
您可以使用
set
数据类型,而不是使用while循环:
通过将列表转换为set
数据类型,您可以确保集合中不会保留重复项,因为集合中不能有重复项,最后您可以将其转换回列表
您可以使用
set
数据类型,而不是使用while循环:
通过将列表转换为set
数据类型,您可以确保集合中不会保留重复项,因为集合中不能有重复项,最后您可以将其转换回列表
使用熊猫可以:
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
# Create a pandas Series from the original list
sandwich_series = pd.Series(sandwich_orders)
# Create a boolean series of items that were not duplicated
unique_bool = ~sandwich_series.duplicated(keep = False)
# 0 False
# 1 True
# 2 False
# 3 False
# 4 True
# 5 False
# 6 True
# dtype: bool
# Select the unique items from the series
sandwich_series[unique_bool].tolist()
# ['pbj ', 'ham ', 'blt ', 'knuckle ']
使用熊猫可以:
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
# Create a pandas Series from the original list
sandwich_series = pd.Series(sandwich_orders)
# Create a boolean series of items that were not duplicated
unique_bool = ~sandwich_series.duplicated(keep = False)
# 0 False
# 1 True
# 2 False
# 3 False
# 4 True
# 5 False
# 6 True
# dtype: bool
# Select the unique items from the series
sandwich_series[unique_bool].tolist()
# ['pbj ', 'ham ', 'blt ', 'knuckle ']
您可以使用一个名为
new_sandwich_orders
的新列表,并存储sandwich_orders
中具有一种外观的项目:
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
finished_sandwiches = []
new_sandwich_orders = []
for s in sandwich_orders:
if sandwich_orders.count(s) == 1:
new_sandwich_orders.append(s)
print("Ordered sandwiches: " , new_sandwich_orders)
while new_sandwich_orders:
complete_sandwiches = new_sandwich_orders.pop()
print("Now making your " + complete_sandwiches.title() + "sandwich")
finished_sandwiches.append(complete_sandwiches.title())
for finished_sandwich in finished_sandwiches:
print("Your " + finished_sandwich.title() + " sandwich is now ready.")
结果将是:
Ordered sandwiches: ['pbj ', 'ham ', 'blt ', 'knuckle ']
Now making your Knuckle sandwich
Now making your Blt sandwich
Now making your Ham sandwich
Now making your Pbj sandwich
Your Knuckle sandwich is now ready.
Your Blt sandwich is now ready.
Your Ham sandwich is now ready.
Your Pbj sandwich is now ready.
您可以使用一个名为
new_sandwich_orders
的新列表,并存储sandwich_orders
中具有一种外观的项目:
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
finished_sandwiches = []
new_sandwich_orders = []
for s in sandwich_orders:
if sandwich_orders.count(s) == 1:
new_sandwich_orders.append(s)
print("Ordered sandwiches: " , new_sandwich_orders)
while new_sandwich_orders:
complete_sandwiches = new_sandwich_orders.pop()
print("Now making your " + complete_sandwiches.title() + "sandwich")
finished_sandwiches.append(complete_sandwiches.title())
for finished_sandwich in finished_sandwiches:
print("Your " + finished_sandwich.title() + " sandwich is now ready.")
结果将是:
Ordered sandwiches: ['pbj ', 'ham ', 'blt ', 'knuckle ']
Now making your Knuckle sandwich
Now making your Blt sandwich
Now making your Ham sandwich
Now making your Pbj sandwich
Your Knuckle sandwich is now ready.
Your Blt sandwich is now ready.
Your Ham sandwich is now ready.
Your Pbj sandwich is now ready.
三明治订单中的“熏牛肉”后面有空格
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
三明治订单中的“熏牛肉”后面有空格
sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
如果您想维持秩序,您可以尝试:
sandwiches = [sandwich_orders, finished_sandwiches, complete_sandwiches]
sandwiches_new = [[i for i in x if i!='pastrami'] for x in sandwiches]
集合本质上是无序的。如果您想保持顺序,可以尝试:
sandwiches = [sandwich_orders, finished_sandwiches, complete_sandwiches]
sandwiches_new = [[i for i in x if i!='pastrami'] for x in sandwiches]
套装本质上是无序的。你已经从
三明治中移除了熏牛肉
,但是你在三明治订单
上循环运行,其中仍然包含熏牛肉
,我不明白。看到我发布的答案,你首先需要从三明治订单
中移除熏牛肉
,然后在它上面循环你已经从三明治中移除了pastrami
,但是你正在运行sandwich\u orders
,其中仍然包含pastrami
,我不明白?看到我发布的答案,你首先需要从sandwich\u orders中移除pastrami
,然后在itBTW上循环,pop()从最后一个位置弹出元素,if语句从第一个元素中删除,根据列表的顺序,最后一个“pastrami”在被删除之前被追加到“finished_sandwiches”中。我在第一个循环上方创建了另一个“while”循环,以引用“三明治订单”列表中的“pastrami”,并将“.remove()”应用于该列表。由于缺少空格,整个时间都在浪费。顺便说一句,pop()会从最后一个位置弹出元素,而if语句会从第一个元素中删除,根据列表的顺序,最后一个“pastrami”会在被删除之前附加到“finished_sandwiches”中。我在第一个循环上方创建了另一个“while”循环,以引用“三明治订单”列表中的“pastrami”,并将“.remove()”应用于该列表。因为没有空间,整个时间都在浪费空间。