无法迭代列表和比较字符串python
我是python编程的新手,我真的需要帮助来比较列表中的值。无法迭代列表和比较字符串python
我有两个列表(饮料,食物)。我没有做两个列表之间的比较。我想要做的是自己比较列表中的所有值。
因此,我必须首先使用for循环和if-else语句比较饮料清单中的所有值。如果饮料中有匹配,我会为食物列表做另一个循环,并使用if-else语句比较食物列表中的值。
这是我的代码:
drinks = []
food = []
drinks.append("lemontea")
drinks.append("coke")
drinks.append("sprite")
drinks.append("orangejuice")
drinks.append("fantagrape")
food.append("nugget")
food.append("pizza")
food.append("chickenwing")
food.append("fries")
food.append("pizza")
for i in drinks:
if i == drinks:
for j in food:
if j == food:
print("fail")
else:
print("success")
我的问题是,我应该会得到一个“失败”输出作为我在食物清单是比萨饼同一项目。此外,我不知道为什么我得到5成功输出时,我希望得到或者只有1'失败'或'成功'输出。
这是我的输出我得到:
success
success
success
success
success
如果任何帮助,给予我将不胜感激。谢谢。
更新: 感谢您的回复,但是,似乎人们对我的问题感到困惑,因此即时提供更多信息。
我必须比较列表中的每个项目的原因是因为这些项目是随机生成的,并且是字符串类型,然后附加到列表中。例如,生成的第一个项目是'00100ABC'等。因此,如果列表中有重复的值/项目,我必须提供错误消息。
#So you have
#drinks = ['lemontea', 'coke', 'sprite', 'orangejuice', 'fantagrape']
#food = ['nugget', 'pizza', 'chickenwing', 'fries', 'pizza']
for i in drinks:
if i == drinks:
for j in food:
if j == food:
print("fail")
else:
print("success")
迭代1
i = 'lemontea'
if i == drinks:
在这里,你正在评估做“我==饮料”, 其中饮料是一个列表对象,我是一个字符串对象。 由于我和饮料不是等价的(它们是具有不同值的不同对象) if语句的计算结果为False,并且 执行else语句,该语句会打印成功。
迭代2
i = 'coke'
if i == drinks
同样,你正在评估与列表对象饮料, 哪个是假的字符串对象“焦”。这一条件适用于所有条件。
你需要做的是改变你的代码索引工作
eg.
drinks[0] = 'lemontea'
drinks[1] = 'coke'
drinks[2] = 'sprite'
ect...
food[0] = 'nugget'
food[1] = 'pizza'
food[2] = 'chickenwing'
比较相同位置的等效利用指数
for i in range(0, len(drinks)):
if drinks[i] == food[i]: #compare index for index
print('Matching index')
else:
print('Index does not match')
现在,这是行不通的,如果在索引中的项目没有在这两个列表中排序,例如,在两个列表中,块可能都不在索引2中。要查看是否在这两个列表“中的”
for item in food: #eg item 0 = 'lemontea'
if item in food and item in drinks:
print(item, 'is in both lists')
else:
print(item, 'does not occur in both lists')
希望帮助无论订单使用的发生的项目。
编辑
for x in food:
while food.count(x) > 1: #breaks loops when only occurrence left.
print("There are", food.count(x), 'occurrence of', x, 'in food')
food.pop(x)
print("deleted one occurrence")
这个循环测试中循环不止一个出现的每个项目,并删除所有,但一个。
替代清洁方法。 - 使用集合,因为它根据定义删除重复。
unique_in_food = list(set(food)) #sets remove all duplicates by definition
now if you want unique items across multiples lists
unique_in_both = list((set(food) | set(drinks))
# set(a) | set(b) finds the union between two sets
嗨,谢谢你的回复。但是,您的解释似乎是将两份食物和饮料列表进行比较。我并没有试图将饮料清单与食物清单进行比较。我试图做的是自己比较饮料单,然后自己比较饮料单 – DanielWinser
为什么你想比较自己的名单?你能举一个你如何使用它的例子吗? – Rosh
即时消息不允许发布实际项目,但我可以告诉你,该项目是随机生成并附加到列表中。但我必须确保物品不一样。因此,我必须比较列表中的每一项,如果有重复,我必须给出错误信息。 – DanielWinser
如果你想检查pizza
值存在于食物清单或没有,你只需要这样做:
def insertValue2List(list, value):
if value not in list:
list.append(value)
else:
print("fail")
drinks = []
food = []
insertValue2List(food, "nugget")
insertValue2List(food, "pizza")
insertValue2List(food, "chickenwing")
insertValue2List(food, "fries")
insertValue2List(food, "pizza")
输出为:
fail
如果你只是想删除列表中的重复值,可以使用示例代码:
print(list(set(food)))
输出是:
['pizza', 'nugget', 'chickenwing', 'fries']
嗨,感谢您的回复。这是否检查其他值,例如块。鸡翅,炸薯条还是只是披萨的价值? – DanielWinser
'set()'元素必须是唯一的,所以如果重复列表元素将被直接删除。 – saul
这将打印上的成功与失败,并删除重复...
drinks = []
food = []
food.append("nugget")
food.append("pizza")
food.append("chickenwing")
food.append("fries")
food.append("pizza")
# let's add some duplicates…
drinks.append("lemontea")
drinks.append("fantagrape")
drinks.append("fantagrape")
drinks.append("coke")
drinks.append("orangejuice")
drinks.append("sprite")
drinks.append("fantagrape")
drinks.append("lemontea")
drinks.append("fantagrape")
drinks.append("sprite")
drinks.append("sprite")
drinks.append("orangejuice")
drinks.append("orangejuice")
drinks.append("coke")
drinks.append("coke")
food.append("chickenwing")
food.append("nugget")
food.append("pizza")
food.append("fries")
food.append("chickenwing")
food.append("nugget")
food.append("fries")
food.append("pizza")
for drink in drinks:
occurences = [index for index, value in enumerate(drinks) if value == drink]
if len(occurences) > 1: # if duplicates occur
print('fail: {}'.format(drink))
occurences.pop() # keep one occurence
[drinks.remove(drink) for o in occurences] # remove the rest
else:
print('success: {}'.format(drink))
for meal in food:
occurences = [index for index, value in enumerate(food) if value == meal]
if len(occurences) > 1: # if duplicates occur
print('fail: {}'.format(meal))
occurences.pop() # keep one occurence
[food.remove(meal) for o in occurences] # remove the rest
else:
print('success: {}'.format(meal))
print(drinks)
print(food)
谢谢,如果我想添加另一个for循环食物列表,该怎么办? – DanielWinser
@DanielWinser是饮料循环的食物循环部分,还是食物循环,它是自己独立的循环? –
这是在一个单独的循环 – DanielWinser
你可以使用组来检查的独特性和交叉
drinks = []
food = []
drinks.append("lemontea")
drinks.append("coke")
drinks.append("sprite")
drinks.append("orangejuice")
drinks.append("fantagrape")
food.append("nugget")
food.append("pizza")
food.append("chickenwing")
food.append("fries")
food.append("pizza")
success = True
独特
if len(set(food)) != len(food):
print('fail: duplicate entry in food')
success = False
if len(set(drinks)) != len(drinks):
print('fail: duplicate entry in drinks')
success = False
两个列表
if set(food) & set(drinks): # if the intersection of both sets is not empty
print('fail: entry in both food and drinks')
success = False
成功!
if success:
print('success')
你肯定有一个错误的是你的情况:'如果我==饮料:'是不正确的。您正在查看列表中的饮料价值。你最终将一个字符串与一个列表进行比较,以确保你的平等,并且肯定会落入你的其他情况。 – idjaw
'如果我==饮料'就像说每个字母是整个字母表。 –
另外,我没有在你的逻辑中进行连接,为什么你必须有这样的嵌套列表。另外,你想要用你的循环逻辑完成什么。这对我来说并不合适。 – idjaw