简体   繁体   English

如何仅显示字典的某些部分?

[英]How do i display only certain part of a dictionary?

I have a dictionary with multiple keys but I only need to print part of it, i don't know how to do it though.我有一本带有多个键的字典,但我只需要打印其中的一部分,但我不知道该怎么做。

The output i have for my dictionary is:我的字典中的 output 是:

[{'vin': '4T1R11AK7NU637019', 'askPrice': 28106.0, 'msrp': 28106.0, 'mileage': 0.0, 'isNew': True,
 'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year': 2022.0,
 'dealerID': 30873, 'color': 'Celestial Silver Metallic', 'interiorColor': 'Black', 'vinDecode': {(A LOT OF THINGS IN HERE}}},

 {'vin': '4T1G31AK7NU45B292', 'askPrice': 0.0, 'msrp': 0.0, 'mileage': 0.0, 'isNew': True,
  'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year': 2022.0,
  'dealerID': 12750, 'color': 'N/A', 'interiorColor': 'N/A', 'vinDecode': {(A LOT OF THINGS IN HERE}}}]

So there are multiple vin s and i would just like to print from vin to interiorColor for every entry;所以有多个vin ,我只想为每个条目从vin打印到interiorColor completely ignoring the vinDecode part.完全忽略了vinDecode部分。 Is there any way I could do it?有什么办法我可以做到吗?

Loop over the items in the dict.循环遍历字典中的项目。 Skip the 'vinDecode' key.跳过“vinDecode”键。

for key, value in mydict.items():
    if key != 'vinDecode':
        print(f'{key}={value}')

You can't do that and you shouldn't anyway because if you can't control the source of this data new keys might appear in the future.您不能这样做,也不应该这样做,因为如果您无法控制这些数据的来源,将来可能会出现新的密钥。

It is best to create a list of keys you are interested in and only print those:最好创建一个您感兴趣的密钥列表并仅打印这些:

my_list_of_dicts = [
    {'vin': '4T1R11AK7NU637019', 'askPrice': 28106.0, 'msrp': 28106.0, 'mileage': 0.0, 'isNew': True,
     'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year': 2022.0,
     'dealerID': 30873, 'color': 'Celestial Silver Metallic', 'interiorColor': 'Black', 'vinDecode': []},
     {'vin': '4T1G31AK7NU45B292', 'askPrice': 0.0, 'msrp': 0.0, 'mileage': 0.0, 'isNew': True,
      'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year': 2022.0,
      'dealerID': 12750, 'color': 'N/A', 'interiorColor': 'N/A', 'vinDecode': []}]

keys_i_am_interested_in = {'vin', 'askPrice', 'interiorColor'}

for d in my_list_of_dicts:
    print({k: v for k, v in d.items() if k in keys_i_am_interested_in})

this will print这将打印

{'vin': '4T1R11AK7NU637019', 'askPrice': 28106.0, 'interiorColor': 'Black'}
{'vin': '4T1G31AK7NU45B292', 'askPrice': 0.0, 'interiorColor': 'N/A'}

You can use .pop() on the dictionary to remove data:您可以在字典上使用.pop()来删除数据:

for item in items:
    item.pop("vinDecode")
    print(item)

You can also create modified list containing dictionaries with vindecode removed.您还可以创建包含删除了vindecode的字典的修改列表。 Later, you can print it.稍后,您可以打印它。


vin_dict = [{'vin': '4T1R11AK7NU637019', 'askPrice': 28106.0, 'msrp': 28106.0, 'mileage': 0.0, 'isNew': True, 'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year':
2022.0, 'dealerID': 30873, 'color': 'Celestial Silver Metallic', 'interiorColor': 'Black', 'vinDecode': ''}, {'vin': '4T1G31AK7NU45B292', 'askPrice': 0.0, 'msrp': 0.0, 'mileage':
0.0, 'isNew': True, 'firstSeen': '2021-11-28', 'lastSeen': '2021-11-28', 'modelName': 'CAMRY', 'brandName': 'TOYOTA', 'year':
2022.0, 'dealerID': 12750, 'color': 'N/A', 'interiorColor': 'N/A', 'vinDecode': ''}]


# Remove specific Key from Dictionary List
modified_vin_dict = [{key : val for key, val in vin.items() if key != 'vinDecode'} for vin in vin_dict]

for d in modified_vin_dict:
    for k, v in d.items():
        print(f'{k}:{v}')

暂无
暂无

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

相关问题 如何仅更改代码的特定部分? - How do I change only a certain part of the code? 如何仅获取 HTML 树的一部分,该部分位于带有特定字符串 BeautifulSoup 的特定标签之上? - How do I get only the part of an HTML tree which is above a certain tag with certain string BeautifulSoup? 将数据框转换为字典时,如何仅提取数据框的一部分作为键? - When converting a dataframe to a dictionary, how do I extract only part of the dataframe to be the key? 如何按字典值的一部分对字典进行排序? - How do I sort a dictionary by a part of the dictionary values? 如何选择 NumPy 数组的某个部分,然后选择下一部分? - How do I select a certain part of NumPy array and then the next part? 如何在具有多个索引/数组的变量中仅循环字典的某些元素? - How do I loop over only certain elements of a dictionary in a variable with multiple indices/arrays? 如何使用复杂列表中的数字作为标题/如何仅打印与某个值匹配的列表的一部分 - How do I use a number from a complex list as a heading / How do I print only part of a list that matches a certain value 如何仅更新字典中的值? - How do I update only values in a dictionary? 如何在 for 循环中计算值作为字典键的一部分 - How do I counter value in a for loop as part of my dictionary key 如何删除 python 字典中的键的一部分 - How do i remove part of a key in a python dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM