3 回答

TA贡献1725条经验 获得超7个赞
item=['ipod','ipad']
bucket=[{'ipad':34,'ipod':36,'iwatch':27},{'ipad':87,'ipod':31,'iwatch':62}]
result_output=[] # Defining for output
counter=0 # For initialising the counter through the bucket's elements
for key in item: # For each row value in column1. Column1 is item.
temp_bucket=dict(bucket[counter]) # For each column's value,it is as a dictionary
if key in temp_bucket: # Checking if the column1's value is present in the dictionary of that row (column2)
length_1=len(bucket)-1 # Need to subtract 1 as python is based on 0 indexing
result_output.append(temp_bucket[key]) # Appending each row output to the result list.
if counter<length_1: # Need to check if there is sufficient element is present in column2. Otherwise throw error for index.
counter=counter+1 # for checking the next element of column2
print(result_output) # 输出列表

TA贡献1998条经验 获得超6个赞
我试过了,它奏效了
对于 df.index 中的 i:
#For Bucket
val_bucket=ast.literal_eval(bucket[i])
for key, val in val_bucket.items():
if my_items[i]== key:
print ("Value",val)
break
因此,我已将那些 JSON 值(串联)转换为 dict 并提取值
添加回答
举报