Python的collections之namedtuple的使用及其优势
类实现:
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的使用及其优势的更多相关文章
- python模块collections中namedtuple()的理解
Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型.相比与list,tuple中的元素不可修改,在映射中可以当键使用.tuple元组的item只能通过index ...
- Python的collections之defaultdict的使用及其优势
user_dict = {} users = ["baoshan1", "baoshan2", "baoshan3","baosh ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
- python的Collections 模块
Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- Python 模块collections
1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...
- 不可不知的Python模块: collections
原文:http://www.zlovezl.cn/articles/collections-in-python/ Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想 ...
- Python中collections模块的使用
本文将详细讲解collections模块中的所有类,和每个类中的方法,从源码和性能的角度剖析. 一个模块主要用来干嘛,有哪些类可以使用,看__init__.py就知道 '''This module i ...
- python 之 Collections模块
官方文档:https://yiyibooks.cn/xx/python_352/library/collections.html 参考: https://blog.csdn.net/songfreem ...
随机推荐
- java基础(6)---面向对象,类,包
一.类 类对象内存: 成员变量和局部变量: 成员变量:类中定义的一些私有变量,表示对象的属性. 局部变量:局部变量是在方法体里创建的,在方法体外是访问不到这个变量的. public class te ...
- 大数据之路week07--day06 (Sqoop 将关系数据库(oracle、mysql、postgresql等)数据与hadoop数据进行转换的工具)
为了方便后面的学习,在学习Hive的过程中先学习一个工具,那就是Sqoop,你会往后机会发现sqoop是我们在学习大数据框架的最简单的框架了. Sqoop是一个用来将Hadoop和关系型数据库中的数据 ...
- Oracle 按指定顺序排序
select * from (select 'Nick' as item from dual union all select 'Viki' as item from dual union all s ...
- “挂起”bug处理执行方案
目的:避免bug状态改为挂起后,就无人问津,导致一直未得到解决.因而影响用户的使用与产品质量较差.
- Linux - 运行 django 时 :django.db.utils.Notsupportederror: urls not supported
运行 django 是异常:django.db.utils.Notsupportederror: urls not supported 原因:sqlite3版本3.7的问题 解决:直接改源码 1. p ...
- Codeforces Round #609 (Div. 2) 【A,B,C】
题意:给一个n<=1e7,找两个合数a和b使得a-b的差为n. 构造a=3n,b=2n,必含有公因子n,只有当n是1的时候是特例. #include<bits/stdc++.h> u ...
- [分享]Passcape Software - Windows Password Recovery
[分享]Passcape Software - Windows Password Recovery https://bbs.pediy.com/thread-245965.htm [[other] ...
- 关于size
关于size它确实可以帮人算内存 但是: 在不会用到整个数组(尤其是在状压的时候) 不要用它,它只能算你申请了多少内存,但算不了会用多少!!! and 有人能告诉我,交题前不好好看看交的哪份代码是什么 ...
- kindle touch 5.1.2 update your kindle 灰色 解决办法
要出差了,于是把抽屉里的老Kindle Touch拿出来想升个级,baidu说多看费电,果断卸了用原生. 但是原生里面升级选项“update your kindle”是灰色的,没法点,怎么办? 试了半 ...
- 13、SparkContext详解
一.SparkContext原理 1.图解 二.SparkContext源码 1.TaskScheduler创建 ###SparkContext.scala // Create and start t ...