簡體   English   中英

Python - 如何僅打印字典列表的鍵

[英]Python - how to print only keys of list of dictionaries

我希望能夠按鍵列出我的詞典列表。

但是我的代碼返回鍵和值如下:

for key in products_dicts[0]:
    print(products_dicts[0].keys())

輸出:

dict_keys(['{"uniq_id": "b6c0b6bea69c722939585baeac73c13d", "sku": "pp5006380337", "name_title": "Alfred Dunner\\u00ae Essential Pull On Capri Pant", "description": "You\'ll return to our Alfred Dunner pull-on capris again and again when you want an updated, casual look and all the comfort you love. \\u00a0 elastic waistband approx. 19-21\\" inseam slash pockets polyester washable imported \\u00a0 \\u00a0 \\u00a0", "list_price": "41.09", "sale_price": "24.16", "category": "alfred dunner", "category_tree": "jcpenney|women|alfred dunner", "average_product_rating": 2.625, "product_url": "http://www.jcpenney.com/alfred-dunner-essential-pull-on-capri-pant/prod.jump?ppId=pp5006380337&catId=cat1002110079&&_dyncharset=UTF-8&urlState=/women/shop-brands/alfred-dunner/yellow/_/N-gkmp33Z132/cat.jump", "product_image_urls": "http://s7d9.scene7.com/is/image/JCPenney/DP1228201517142050M.tif?hei=380&wid=380&op_usm=.4,.8,0,0&resmode=sharp2&op_usm=1.5,.8,0,0&resmode=sharp", "brand": "Alfred Dunner", "total_number_reviews": 8, "Reviews": [{"User": "fsdv4141", "Review": "You never have to worry about the fit...Alfred Dunner clothing sizes are true to size and fits perfectly. Great value for the money.", "Score": 2}, {"User": "krpz1113", "Review": "Good quality fabric. Perfect fit. Washed very well no iron.", "Score": 4}, {"User": "mbmg3241", "Review": "I do not normally wear pants or capris that have an elastic waist, but I decided to try these since they were on sale and I loved the color. I was very surprised at how comfortable they are and wear really well even wearing all day. I will buy this style again!", "Score": 4}, {"User": "zeqg1222", "Review": "I love these capris! They fit true to size and are so comfortable to wear. I am planning to order more of them.", "Score": 1}, {"User": "nvfn3212", "Review": "This product is very comfortable and the fabric launders very well", "Score": 1}, {"User": "aajh3423", "Review": "I did not like the fabric. It is 100% polyester I thought it was different.I bought one at the store apprx two monts ago, and I thought it was just like it", "Score": 5}, {"User": "usvp2142", "Review": "What a great deal. Beautiful Pants. Its more than I expected.", "Score": 3}, {"User": "yemw3321", "Review": "Alfred Dunner has great pants, good fit and very comfortable", "Score": 1}], "Bought With": ["898e42fe937a33e8ce5e900ca7a4d924", "8c02c262567a2267cd207e35637feb1c", "b62dd54545cdc1a05d8aaa2d25aed996", "0da4c2dcc8cfa0e71200883b00d22b30", "90c46b841e2eeece992c57071387899c"]}\n'])

如果我在代碼中省略索引,則會出現以下錯誤:

AttributeError                            Traceback (most recent call last)
<ipython-input-12-c723fc75d649> in <module>
      2 
      3 for key in products_dicts[0]:
----> 4     print(products_dicts.keys())

AttributeError: 'list' object has no attribute 'keys'

對我來說奇怪的是,如果我運行這段代碼:

print(type(reviewers_dicts[0]))

它返回:

<class 'dict'>

這是數據結構問題嗎?

您的兩個 for 循環都具有以下格式:

for item in things:
    print(things.keys())

如果你的things在清單中,

things[0].keys()

將是第一項的keys 要迭代所有項目,請使用您在 for 循環中選擇的名稱:

for item in things:
    print(item.keys())
    #     ^^^^ --- not all things, just the current item

像這樣嘗試:

listis = [
    {
        1: "One"
        , 11: "OneOne"
    }
    , {2: "Two"}
    , {3: "Three"}
    , {4: "Four"}
]

for obj in listis:
    for key in obj.keys():
        print(key)

您將在字典列表中打印每個字典的所有鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM