类实现:

class User:
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height user = User(name="baoshan", age=31, height=170)
print(user.name, user.age, user.height)

namedtuple实现

方式1:

from collections import namedtuple

User = namedtuple("User", ["name", "age", "height"])
user = User(name="baoshan", age=31, height=170)
print(user.name, user.age, user.height)

方式2:

from collections import namedtuple

User = namedtuple("User", ["name", "age", "height"])
user_tuple = ("baoshan", 31, 170)
user = User(*user_tuple)
print(user.name, user.age, user.height)

namedtuple的优势

# namedtuple直接可以创建一个类
# 优势1: 代码简洁
# 优势2: 内存小,效率高 数据处理方面很有优势
user_info_dict = user._asdict()
print(user_info_dict) # namedtuple可以转换为dict
name, age, *others = user
print(name, age, *others) # namedtuple可以拆包

Python的collections之namedtuple的使用及其优势的更多相关文章

  1. python模块collections中namedtuple()的理解

    Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型.相比与list,tuple中的元素不可修改,在映射中可以当键使用.tuple元组的item只能通过index ...

  2. Python的collections之defaultdict的使用及其优势

    user_dict = {} users = ["baoshan1", "baoshan2", "baoshan3","baosh ...

  3. Python的collections模块中namedtuple结构使用示例

      namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...

  4. python的Collections 模块

    Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...

  5. Python中collections模块

    目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...

  6. Python 模块collections

    1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...

  7. 不可不知的Python模块: collections

    原文:http://www.zlovezl.cn/articles/collections-in-python/ Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想 ...

  8. Python中collections模块的使用

    本文将详细讲解collections模块中的所有类,和每个类中的方法,从源码和性能的角度剖析. 一个模块主要用来干嘛,有哪些类可以使用,看__init__.py就知道 '''This module i ...

  9. python 之 Collections模块

    官方文档:https://yiyibooks.cn/xx/python_352/library/collections.html 参考: https://blog.csdn.net/songfreem ...

随机推荐

  1. Java注解annotation : invalid type of annotation member

    前言 首先,关于注解的介绍就不多描述了,网上有很多这方面的资料.本文主要是介绍如何处理标题中遇到的问题:invalid type of annotation member ? 正文 Annotatio ...

  2. memcached的缺点

    上篇博客说了为什么引入memcached,主要讲述了memcached的优点,接下来就是我们在使用中必须要注意的内容,memcached的缺点,只有正确认识它,才能运用自如,接下来先看一下memcac ...

  3. myeclipse修改内存大小不足tomcat内存不足

    myeclipse修改内存大小不足 打开Windows-> Preferences -> Java->Installed JREs 点击右侧的jdk,然后点击“Edit”按钮 Def ...

  4. 【测试工具】moco入门(一)

    转自:https://www.cnblogs.com/tangqiu/p/9493147.html 简单来说,Moco就是解决了开发前端时没有后端支持,开发接口时依赖没有到位的尴尬场景.当然Moco的 ...

  5. C语言实验1—— C中的指针和结构体

    问题 实现一个算法,检测单链表中是否有环,如果有环还要得到环的入口. 分析 判断是否有环:快慢指针法(也叫“龟兔赛跑”),慢指针每次移动一位,快指针每次移动两位,如果有环,他们一定会相遇. 求环的入口 ...

  6. vue中input输入第一个字符时,光标会消失,需要再次点击才能输入

    vue中input输入第一个字符时,光标会消失,需要再次点击才能输入 在这里我犯了一个小错误,v-if语法比较倾向于一次性操作,当input获取焦点时,v-if判断为true,立即刷新数据,进行渲染, ...

  7. sort函数使用自定义数据排序使用

    package main import ( "fmt" "sort" ) type ServerSlice []Server type Server struc ...

  8. SpringMVC的文件上传与下载

    1. 单文件上传 配置jsp页面 <%@ page contentType="text/html;charset=UTF-8" language="java&quo ...

  9. Hive 数据类型与文件格式

    一.基本数据类型 1.基本数据类型 Tinyint  1byte有符号整数  比如20 Smalint 2byte有符号整数 比如20 Int          4byte有符号整数 比如20 Big ...

  10. 【一起来烧脑】一步学会AngularJS系统

    AngularJS是一个JavaScript框架 一个用JavaScript编写的库 [外链图片转存失败(img-JUTh171K-1563341248796)(https://upload-imag ...