Python词典中的条件
我正在尝试打印列表-Python词典中的条件,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我正在尝试打印列表-x中包含的电子邮件,条件是它们的类型为-prospect。请告诉我在这种情况下正确的语法是什么 s = {'domain': 'truecustomer.com', 'webmail': False, 'result': 5, 'limit': 5, 'offset': 0, 'companyName': 'True Customer Group', 'emails': [{'email': 'john@truecustomer.
x
中包含的电子邮件,条件是它们的类型为-prospect
。请告诉我在这种情况下正确的语法是什么
s = {'domain': 'truecustomer.com', 'webmail': False, 'result': 5, 'limit': 5, 'offset': 0, 'companyName': 'True Customer Group',
'emails':
[{'email': 'john@truecustomer.com', 'type': 'email', 'status': 'verified'},
{'email': 'jim@truecustomer.com', 'type': 'email', 'status': 'verified'},
{'email': 'johnl@truecustomer.com', 'type': 'prospect', 'status': 'verified', 'firstName': 'John', 'lastName': 'Leeman', 'position': 'Co-Founder, Managing Director', 'sourcePage': 'https://www.linkedin.com/in/john-leeman-bba176'},
{'email': 'john-leeman@truecustomer.com', 'type': 'prospect', 'status': 'verified', 'firstName': 'John', 'lastName': 'Leeman', 'position': 'Co-Founder, Managing Director', 'sourcePage': 'https://www.linkedin.com/in/john-leeman-bba176'},
{'email': 'j.leeman@truecustomer.com', 'type': 'email', 'status': 'verified'}]}
x = (s['emails']) #It's a list
for item in x: #item is dict
a = item['type']
if a = prospect:
print(email)
Output - File "output.py", line 20
if a = prospect:
^
SyntaxError: invalid syntax
尝试:
在这种情况下,您可能想使用
if
(条件逻辑)而不是for
(迭代)。为什么要使用for
?你应该使用if
。上面的代码充满了错误。在您的情况下,最好从Python基础知识开始,相等比较运算符为==。字典中的键和值都是必须使用的字符串。所以,你必须写:“潜在客户”和“电子邮件”,如果a==“潜在客户”:
for item in x:
if item['type'] == 'prospect':
print('%s\n'%item['email'])