[英]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.