[英]how to make a list of matching items from a list in python?
我有一份清單。 我想要匹配項並在列表中打印匹配項
假使,假設
dataReader= [
{
"id": 1,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 2,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 3,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 4,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 5,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 6,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
}
]
我有這個項目清單。 我想從列表中過濾 "order_number": 53010 匹配項並制作另一個匹配項的字典列表
我想要這樣的結果
dataReader
[
"id": 1,
"user_payment_id": 2,
"user_id": 1,
"name": "string",
"email": "string",
"order_number": 53010 [
{
"item_id": 1,
"item_name": "string",
"item_price": 0,
"item_description": "string"
},
{
"item_id": 2,
"item_name": "string",
"item_price": 0,
"item_description": "string"
},
{
"item_id": 3,
"item_name": "string",
"item_price": 0,
"item_description": "string"
}
]
"product_is_active": true,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string"
}
]
我寫了這段代碼,
dataReader= [
{
"id": 1,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 2,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 3,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 4,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 5,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
},
{
"id": 6,
"user_payment_id": 2,
"user_id": 1,
"email": "string",
"order_number": 53010,
"billing_first_name": "string",
"billing_last_name": "string",
"billing_phone_number": "string",
"billing_country": "string",
"item_id": 1,
"item_name": "string",
"item_price": 0,
"description": "string"
}
]
count = 0
for row in dataReader:
bookList= dict()
#bookList[row[0]]= row[1]
#print(row)
#for k,v in bookList.items():
#print(k,v)
num_0f_rows=len(dataReader)
print("rows=",num_0f_rows)
row=0
if row< num_0f_rows:
question_id_student_user=dataReader[row].get("order_number")
new_list=[]
for i in dataReader:
if i["order_number"]==question_id_student_user:
new_list.append(i)
print(new_list)
#print(question_id_student_user)
我找到了結果,但無法將結果保存在新列表中。 結果沒有顯示。幫幫我
您可以通過執行以下操作按鍵值過濾 dict 列表:
filtered_list = [element for element in dataReader if element['order_number'] == 53010]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.