Python 如果在my_friends.items()中有

Python 如果在my_friends.items()中有,python,python-3.x,Python,Python 3.x,以下是我的代码: my_friends = { "Jose": 6, "Rolf": 12, "Anne": 6 }; for e, v in my_friends.items(): a = ('Jose', 6); if a in my_friends.items(): print("yes"); 运行此代码时,会打印3次“是”。这是令人困惑的,因为当您打印my_friends.items()时,您会得到[('Jose',6),('R

以下是我的代码:

my_friends = {
  "Jose": 6,
  "Rolf": 12, 
  "Anne": 6
};



for e, v in my_friends.items():

    a = ('Jose', 6);

    if a in my_friends.items():

        print("yes");

运行此代码时,会打印3次“是”。这是令人困惑的,因为当您打印my_friends.items()时,您会得到[('Jose',6),('Rolf',12),('Anne',6)],如果是这种情况,那么不应该打印一次yes,因为('Jose',6)在列表中显示一次,即my_friends.items()?这是个新问题,所以请原谅这个潜在的无知问题

您正在使用
e
v
遍历
my_friends.items()
,但您没有使用这些变量。您所要做的就是测试
('Jose',6)
是否在
my_friends.items()
中,因此它自然会对3次迭代中的每一次执行相同的操作

只要去掉环,你就没事了

my_friends = { "Jose": 6, "Rolf": 12, "Anne": 6 }
a = ('Jose', 6)
if a in my_friends.items():
    print("yes")

y中的x检查列表中是否存在元素。你不需要循环这张支票

a = ('Jose', 6)

if a in my_friends.items():

print("yes")

应该在循环之外,并且只检查一次。

您三次得到“是”的原因是您正在从我的朋友那里获取所有项目现在有三个项目,现在在下一行您正在初始化一个到('Jose',6),这也将做三次。
同样的条件被检查并验证为真,因为('Jose',6)出现在my_friends中,所以你会得到三次“yes”。

你给出的代码没有正确的缩进,所以让我先修正一下:

my_friends = { "Jose": 6, "Rolf": 12, "Anne": 6 }

for e, v in my_friends.items():
    a = ('Jose', 6)
    if a in my_friends.items():
        print("yes")
让我告诉你你想做什么。这将使你更容易理解正在发生的事情

1) 将创建一个包含三项的词典。
2) 使用
for
循环对字典进行迭代(总共三次迭代)
3) 在每次迭代中,检查字典中是否存在
('Jose',6)

4)
('Jose',6)
确实存在于字典中,因此
是
在每次迭代中打印

如果要检查字典中是否存在
('Jose',6)
,只需执行以下操作,而无需执行
for
循环

if a in my_friends.items():
    print("yes")
或者,如果您确实想使用
for
循环,可以执行以下操作:

a = ('Jose', 6)
for e, v in my_friends.items():
    if a == (e,v):
        print("yes")

顺便说一句,您不需要在for循环内部编写
a=('Jose',6)
,您可以在循环外部编写它,如上所示。关键是,在循环中一次又一次地为
a
分配相同的值是多余的。您只需在循环外执行一次即可

在您的情况下,您正在检查一个项目的成员资格
('Jose',6)
,以便在字典
my_friends
上进行完整的迭代,并且在每次迭代中都是
真实的
,因为该项目是真正的成员资格(即
('Jose',6)
我的朋友
的成员

注意:检查收藏中同一物品的成员身份的次数并不重要

如果它存在于集合中,则如果使用运算符中的执行membeship检查,则始终会得到
True

看看下面的例子

>>> l = [1, 2, 3, 4, 5, 6]
>>>
>>> for item in l:
...     if 1 in l:
...         print('yes');
...
yes
yes
yes
yes
yes
yes
>>>
>>> 2 in l
True
>>>
>>> 7 in l
False
>>>
>>> # Dictionary example
...
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>>
>>> d.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
>>>
>>> list(d.items())
[('a', 1), ('b', 2), ('c', 3)]
>>>
>>> ('a', 1) in d.items()
True
>>> ('a', 4) in d.items()
False
>>>
更好的解决方案是将您的程序制作成这样

>>> a = ('Jose', 6)
>>> for e, v in my_friends.items():
...     if a == (e, v):
...         print('yes');
...
yes
>>>

为什么它会重复3次?它不应该只迭代一次,然后找到一个,然后报告回来吗?因为
my_friends.items()
tuple中有3个项。所以我一定误解了事情是如何迭代的。它不是迭代每个元素吗?在这种情况下,每个元组?所以它应该遍历每个元组并打印一次yes?我知道我错了,但不明白@通过使用dict
my_friends
items
方法使用3138766,您可以将dict转换为元组列表
[(“Jose”,6),(“Rolf”,12),(“Anne”,6)]
。由于您只想知道元组
('Jose',6)
是否在前面提到的元组列表中,因此只需使用
in
操作符在
if
语句中对其进行测试。这里不需要
for
循环。@用户3138766,因为在这里您实际使用的是变量
e
,该变量在3次迭代中的每一次都被分配了
my_friends
dict的键。在这些键中,只有一个满足
e==“Jose”
语句中的
if
测试,所以
print(“6”)
只执行一次。如果我们想用布尔值检查,比如:if e==“Jose”,那么我们需要一个for循环来迭代,对吗?问题是
my_friends.items()
返回元组列表,其中每个元组都是一对(键、值)。所以
my_friends.items()
将返回
[('Jose',6),('Rolf',12),('Anne',6)]
。因此从技术上讲,
6
不是给定列表中的元素。事实上,它是列表元素中的一个成员(在
('Jose',6)
中的第二个成员)。我从您上次的评论中得到它。非常感谢你!!因此,如果我们运行(如果my_friends.items()中的“Jose”),它将不会打印,因为“Jose不是元素。但是当我们运行时(如果my_friends中的“Jose”)它将运行,因为my_friends代表dict的键my_friends
my_friends
是一个字典。当你在my_friends
中说
x时,它将检查
x
是否是字典中的键。现在
'Jose'
作为键存在于
my_friends
,因此
'Jose'在my_friends
中返回
True
.While
my_friends.items()
将返回前面评论中解释的元组列表。是的,没错!@user3138766 Yea别担心:p你能用另一种方式解释吗?我不明白。如果这是真的,那么为什么在my_friends.items()中为e,v会返回元组列表:如果e=“Jose”:print(“6”)不打印6三次,只打印一次?让我逐行解释一下。y_friends={“Jose”:6,“Rolf”:12,“Anne”:6}####这是包含键和值的三个元素长度的字典。对于我的朋友中的e,v。items():#这个迭代器从循环中的dictionanry中获取键和值。a=('Jose',6)#这里您正在用元组值('J)初始化变量a