Joke Collection Website - Mood Talk - On Python's Named Tuples
On Python's Named Tuples
Python introduced the factory function collections.namedtuple to construct tuples with field names.
(1) declaration and instantiation
We usually construct named tuples like this:
The following is an example of declaring a named tuple and its instantiation method:
Running results:
Here, a named tuple is created through collections.namedtuple to represent a playing card. Rank stands for the number of playing cards and suit stands for suit. You can instantiate a named tuple by field name or location. Select_card here means that a spade 10 has been drawn.
(2) Unique attributes and methods
Named tuples also have the following unique properties and methods.
Running results:
In addition, the corresponding attribute values in the named tuple can also be passed. Grammar:
Running results:
- Previous article:Translation of excerpts from Sima Qian to Ren Anxin.
- Next article:Cao Cao's character
- Related articles
- 1 hour from Shenzhen! Two cities in Guangdong, with good environment and low prices, are very suitable for sheltering from the cold and providing for the elderly.
- Thoughts on riding a goose.
- What famous sayings are shorter?
- When did this dialect originate?
- Good morning: talk about why a girl should work hard. 216.1.21
- Is it time to let go? Tell me.
- Write a word, I hope you will be treated gently by the world.
- Shenzhen Baiheng Office Supplies Co., Ltd. (Zuo Shang) What about this company? Baoan branch!
- Pinggu District Fair Schedule 2019 Pinggu District Fair Introduction
- If I miss this life, there will be no me in the afterlife.