PYTHONL Telegram 5126
This media is not supported in your browser
VIEW IN TELEGRAM
🐍 Python трюк: сортировка namedtuple

Вместо словарей можно использовать namedtuple для хранения структурированных данных, а потом легко сортировать по любому полю.


from collections import namedtuple

Name = namedtuple("Name", ["first", "last"])
names = [
Name("Mike", "Driscoll"),
Name("Zahna", "Brown"),
Name("James", "Williams")
]

# Сортировка по имени
print(sorted(names, key=lambda n: n.first))

# Сортировка по фамилии
print(sorted(names, key=lambda n: n.last))


@pythonl
22👍12🔥7



tgoop.com/pythonl/5126
Create:
Last Update:

🐍 Python трюк: сортировка namedtuple

Вместо словарей можно использовать namedtuple для хранения структурированных данных, а потом легко сортировать по любому полю.


from collections import namedtuple

Name = namedtuple("Name", ["first", "last"])
names = [
Name("Mike", "Driscoll"),
Name("Zahna", "Brown"),
Name("James", "Williams")
]

# Сортировка по имени
print(sorted(names, key=lambda n: n.first))

# Сортировка по фамилии
print(sorted(names, key=lambda n: n.last))


@pythonl

BY Python/ django


Share with your friend now:
tgoop.com/pythonl/5126

View MORE
Open in Telegram


Telegram News

Date: |

With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings. It’s easy to create a Telegram channel via desktop app or mobile app (for Android and iOS): The public channel had more than 109,000 subscribers, Judge Hui said. Ng had the power to remove or amend the messages in the channel, but he “allowed them to exist.” The channel also called on people to turn out for illegal assemblies and listed the things that participants should bring along with them, showing prior planning was in the works for riots. The messages also incited people to hurl toxic gas bombs at police and MTR stations, he added. How to create a business channel on Telegram? (Tutorial)
from us


Telegram Python/ django
FROM American