Я попытался напечатать ключ dict после сортировки по приведенному ниже коду. все работало нормально
import pprint
from operator import itemgetter
cars = [
{'Name':'Ka','Year Introduced':1996,'Production of the current model':2014,'Generation':'3rd Generation','Vehicle Information':'Developed by Ford Brazil as a super mini car'},
{'Name':'Fiesta','Year Introduced':1976,'Production of the current model':2017,'Generation':'7th Generation (Mark VIII)','Vehicle Information':'Ford\'s long running subcompact line based on global B-car Platform'},
{'Name':'Focus','Year Introduced':1998,'Production of the current model':2018,'Generation':'3rd Generation (Mark III)','Vehicle Information':'Ford\'s Compact car based on global C-car platform'},
{'Name':'Mondeo','Year Introduced':1992,'Production of the current model':2012,'Generation':'2nd Generation (Fusion)','Vehicle Information':'Mid sized passenger sedan with "One-Ford" design based on CD4 platform.'},
{'Name':'Taurus','Year Introduced':1986,'Production of the current model':2009,'Generation':'6th Generation','Vehicle Information':'Full sized car based on D3 platform'},
{'Name':'Fiesta ST','Year Introduced':2013,'Production of the current model':2013,'Generation':'1st Generation (6th Generation)','Vehicle Information':'Fiesta\'s high performance factory tune'},
{'Name':'Focus RS','Year Introduced':2015,'Production of the current model':2015,'Generation':'1st Generation (3rd Generation)','Vehicle Information':'Special high performance Focus developed by SVT'},
{'Name':'Mustang','Year Introduced':1964,'Production of the current model':2014,'Generation':'6th Generation','Vehicle Information':'Ford\'s long running pony/muscle car'},
{'Name':'GT','Year Introduced':2004,'Production of the current model':2016,'Generation':'2nd Generation','Vehicle Information':'Ford\'s limited production super car inspired by the legendary race car GT40'}
]
# Creates a new dictionary with the name value as the key, and the dictionary as the value
def newDict(carlist):
mynewDict ={}
for car in carlist:
mynewDict[car['Name'],car['Year Introduced']] = car
return mynewDict
def alphaCars(mycardict):
carlist = list(mycardict.keys())
carlist.sort()
#print (carlist)
return carlist
mydict = newDict(cars)
#pprint.pprint (alphaCars(mydict))
for i in sorted (newDict(cars).keys()):
print(i)
Я попробовал то же самое, чтобы получить отсортированное «значение» из dict. попробовал однострочный код, упомянутый в переполнении стека, и я не могу это сделать. получение нижеупомянутой ошибки
Ошибка: не поддерживается между экземплярами dict и dict
я создал dict из экземпляров другого dict. Я не использовал лямбда, просто получить желаемый результат с помощью лямбды. Хотел проверить, можно ли сортировать значения dict без лямбды.