Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有Json、If语句的Python_Python_Json - Fatal编程技术网

带有Json、If语句的Python

带有Json、If语句的Python,python,json,Python,Json,我有下面的json代码和一个列表 我想做一个for循环或if语句 if label in selected_size: fsize = id selected_size[] 选定尺寸: [7, 7.5, 4, 4.5] 在json中: removed 我不知道怎么做。您需要访问列表,然后再访问dict,例如: json_data = [{'id': '91', 'label': '10.5', 'price': '0', 'oldPrice': '0', 'products': ['

我有下面的json代码和一个列表

我想做一个for循环或if语句

if label in selected_size:
  fsize = id

selected_size[]
选定尺寸:

[7, 7.5, 4, 4.5]
在json中:

removed

我不知道怎么做。

您需要访问列表,然后再访问dict,例如:

json_data = [{'id': '91', 'label': '10.5', 'price': '0', 'oldPrice': '0', 'products': ['81278']}, {'id': '150', 'label': '9.5', 'price': '0', 'oldPrice': '0', 'products': ['81276']}, {'id': '28', 'label': '4', 'price': '0', 'oldPrice': '0', 'products': ['81270']}, {'id': '29', 'label': '5', 'price': '0', 'oldPrice': '0', 'products': ['81271']}, {'id': '22', 'label': '8', 'price': '0', 'oldPrice': '0', 'products': ['81274']}, {'id': '23', 'label': '9', 'price': '0', 'oldPrice': '0', 'products': ['81275']}, {'id': '24', 'label': '10', 'price': '0', 'oldPrice': '0', 'products': ['81277']}, {'id': '25', 'label': '11', 'price': '0', 'oldPrice': '0', 'products': ['81279']}, {'id': '26', 'label': '12', 'price': '0', 'oldPrice': '0', 'products': ['81280']}]
fsize = []
select_size = [7, 7.5, 4, 4.5]
[float(i) for i in select_size] #All select_size values to float value
for size in json_data:
    if float(size['label']) in select_size: #For compare it i need float(size['label']) for convert to float.
        fsize.append(size['id']) #Add to list

print(fsize) #Print all list, i get only 28


您希望从此列表中获得什么?
size['label']
size['id']
…!?另外,
size['label']
是一个字符串,但是
selected\u size
是数字。你需要将其中一个转换成另一个,这样才能起作用。@deceze你的评论帮助我回答。非常感谢,这正是我想要的。
json_data = [{'id': '91', 'label': '10.5', 'price': '0', 'oldPrice': '0', 'products': ['81278']}, {'id': '150', 'label': '9.5', 'price': '0', 'oldPrice': '0', 'products': ['81276']}, {'id': '28', 'label': '4', 'price': '0', 'oldPrice': '0', 'products': ['81270']}, {'id': '29', 'label': '5', 'price': '0', 'oldPrice': '0', 'products': ['81271']}, {'id': '22', 'label': '8', 'price': '0', 'oldPrice': '0', 'products': ['81274']}, {'id': '23', 'label': '9', 'price': '0', 'oldPrice': '0', 'products': ['81275']}, {'id': '24', 'label': '10', 'price': '0', 'oldPrice': '0', 'products': ['81277']}, {'id': '25', 'label': '11', 'price': '0', 'oldPrice': '0', 'products': ['81279']}, {'id': '26', 'label': '12', 'price': '0', 'oldPrice': '0', 'products': ['81280']}]
fsize = []
select_size = [7, 7.5, 4, 4.5]
[float(i) for i in select_size] #All select_size values to float value
for size in json_data:
    if float(size['label']) in select_size: #For compare it i need float(size['label']) for convert to float.
        fsize.append(size['id']) #Add to list

print(fsize) #Print all list, i get only 28