简体   繁体   English

Python - 如何仅打印字典列表的键

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

I would like to be able my list of dictionaries by key.我希望能够按键列出我的词典列表。

However my code is returning the key and the value as follows:但是我的代码返回键和值如下:

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

Output:输出:

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'])

If I omit the index in my code I get the following error:如果我在代码中省略索引,则会出现以下错误:

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'

What is strange about that to me is that if I run this code:.对我来说奇怪的是,如果我运行这段代码:

print(type(reviewers_dicts[0]))

It returns:它返回:

<class 'dict'>

Is this a data structure issue?.这是数据结构问题吗?

Both of your for loops have this format:您的两个 for 循环都具有以下格式:

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

If your things are in a list,如果你的things在清单中,

things[0].keys()

will be the keys of the first item.将是第一项的keys To iterate all the items, use the name you choose in the for loop:要迭代所有项目,请使用您在 for 循环中选择的名称:

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

Try like this:像这样尝试:

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

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

You will print all keys of each dictionary in list of dictionaries.您将在字典列表中打印每个字典的所有键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM