Почему вы предпочитаете Java 8 Stream API вместо прямых запросов Hibernate / SQL при работе с БД

Я думаю, что проще всего отсортировать ключ dict и сохранить отсортированную пару: значение в новом dict.

dict1 = {'renault': 3, 'ford':4, 'volvo': 1, 'toyota': 2} 
dict2 = {}                  # create an empty dict to store the sorted values
for key in sorted(dict1.keys()):
    if not key in dict2:    # Depending on the goal, this line may not be neccessary
        dict2[key] = dict1[key]

Чтобы сделать его более понятным:

dict1 = {'renault': 3, 'ford':4, 'volvo': 1, 'toyota': 2} 
dict2 = {}                  # create an empty dict to store the sorted     values
for key in sorted(dict1.keys()):
    if not key in dict2:    # Depending on the goal, this line may not be  neccessary
        value = dict1[key]
        dict2[key] = value
20
задан Olivier Grégoire 10 April 2017 в 09:34
поделиться