Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NamedTuple to name tuples in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use NamedTuple naming tuples in python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Create a new python file named py3_namedtuple.py, and write the operation code in this file:

# namedtuple is like a normal tuple tuple # but it is more readable # is a high-performance container data type from collections import namedtuple# defines a normal color tuple # Red:55 Green:155 Blue:255color = (55155255) # here we print the value print (color [0]) # 5percent when you look back at the color [0] # code after a long time, it's hard to understand that it represents red. The value of the color # is not readable # and the way we came up with is to adjust it to dictionarycolor_dict = {'red':55. 'green':155,'blue':255} # getting the red value print (color_dict [' red']) # 5colors is a bit more readable # but the dictionary is variable # if the red value is changed # it's easy to cause confusion # here we look at the way to use namedtuple: Color = namedtuple ('Color', [' red','green') 'blue']) color = Color (55155255) # print red value print (color.red) # 5 "We define a white corresponding rgbwhite = Color (255255255) # to get the value of blue in white print (white.blue) # 25" here, if you use a dictionary, you can imagine that # readability and ease of use of the code will be very poor. This is how to use NamedTuple named tuples in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report