简体   繁体   English

如何在一行中打印您选择的字典中的值? 在 Python 中

[英]How to print values from dictionary of your choice in a single line ? IN PYTHON

Suppose this is a dictionary : {'name': 'Instagram', 'follower_count': 346, 'description': 'Social media platform', 'country': 'United States'}假设这是一本字典: {'name': 'Instagram', 'follower_count': 346, 'description': 'Social media platform', 'country': 'United States'}

and i want my output like : Instagram, Social media platform, United States我想要我的输出,比如: Instagram, Social media platform, United States

How do I achieve this?我如何实现这一目标?

I think this is what you're looking for?我想这就是你要找的吗?

import operator

items_getter = operator.itemgetter('name', 'description', 'country')
print(', '.join(items_getter(dictionary)))

Use the key in condition to whatever value you want to eliminate使用条件中的键来消除您想要消除的任何值

for i in thisdict:
    if i == "follower_count":
        continue
    else:
        print(thisdict[i])

Or you may also use .items method to get key,values and then continue或者您也可以使用 .items 方法获取键值,然后继续

for k,v in thisdict.items():
    if k=="follower_count":
        continue
    else:
        print(v)

This is the simplest way you can get what you want:这是您获得所需内容的最简单方法:

dct = {
    'name': 'Instagram', 
    'follower_count': 346, 
    'description': 'Social media platform', 
    'country': 'United States'
}

print(f'{dct['name']}, {dct['description']}, {dct['country']}')

Output:输出:

Instagram, Social media platform, United States

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

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