类实现:

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. dict排序

    根据dict值排序 c = {1:10,2:9,3:8} c = sorted(c.items(), key=lambda d: d[1], reverse=1) reverse=1 从大到小排列 起 ...

  2. win10 64下anaconda4.2.0(python3.5)

    python环境:win10 64下anaconda4.2.0(python3.5).安装tensorflow过程是在Anaconda Prompt中进行安装 1:打开Anaconda Prompt ...

  3. Linux 服务器性能出问题,排查下这些参数指标

    taozj马哥Linux运维 一个基于 Linux 操作系统的服务器运行的同时,也会表征出各种各样参数信息.通常来说运维人员.系统管理员会对这些数据会极为敏感,但是这些参数对于开发者来说也十分重要,尤 ...

  4. python_并发编程——多进程

    from multiprocessing import Process import os def func1(): print('子进程1',os.getpid()) #子进程:获取当前进程的进程号 ...

  5. PHP——curl设置请求头需要注意哪些

    前言 在设置这个请求头上踩了一些坑,此文记录下. 步骤 设置请求头 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 请求头写法 一定不要忘记:不然进行请求 ...

  6. java中equals和==的使用

    ==可以用来比较基本数据类型和引用数据类型,在进行基本数据类型的比较时,比较的具体的值,进行引用数据类型比较,比较的是引用指向对象在内存中的地址,但是String进行比较需要注意 package cn ...

  7. 大小端示例-arm c51

    大小端系列文章https://blog.csdn.net/liming0931/article/details/100016425 MDK(Keil5,STM32F407)C语言: #include  ...

  8. LightOJ - 1246 - Colorful Board(DP)

    链接: https://vjudge.net/problem/LightOJ-1246 题意: You are given a rectangular board. You are asked to ...

  9. pwn第一周

    源码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> void setbufs() { set ...

  10. HEXO的使用

    本文将总结性的介绍如何建立自己的github.io博客,后续会持续补充,进阶.感谢baixin提供的参考文章. 技术选型为github+hexo+idea,首先最简单的阐述下这个东西都干嘛的 1. 技 ...