【发布时间】:2020-04-10 19:54:51
【问题描述】:
在这两个条件下都存在此数据,但我的循环没有获取第二个字典的数据,它总是在第一个字典条件后终止。
在此处输入代码
if form and len(form) > 0:
d1 = json.loads(form)
d2 = list(d1.keys())
v2 = list(d1.values())
for x in range(len(d1)):
if type(v2[x]) == dict and v2[x]['key']:
selected_value = v2[x]['value']
print("print 1")
elif type(v2[x]) == dict and v2[x]['customer_id']:
customer_names = v2[x]['customer_name']
print("Print 2")
【问题讨论】:
-
修正你的意图 - 如果
type(v2[x]) == list,你怎么期望type(v2[x])也将成为dict? -
您是说您的字典符合
if和elif语句的条件吗?因此您希望它同时打印您的print语句?因为如果是这样,那不是if/elif/else阻止works 的方式。如果您想/需要分别检查多个条件并为每个满足的条件做一些事情,您需要使用多个if子句。 -
您能否在
d1 = json.loads(form)之后添加print(d1)并分享结果以便更好地理解 -
@Limbo -- 我只分享三行,因为数据量很大 - d1 : {'outlet': {'customer_id': '1238', 'customer_name': 'Nzmxzmm', ' field_type': 'autocomplete'}, 'coimbatore': {'key': '1', 'value': 'Demo', 'field_type': 'select'}} d1 : {'outlet': {'customer_id': '1100', 'customer_name': '测试值', 'field_type': 'autocomplete'}, 'coimbatore': {'key': '1', 'value': 'Demo', 'field_type': 'select' }}
标签: python python-3.x loops dictionary conditional-statements