数据结构和序列(Python for data analysis,2ed)

本文介绍了Python中元组的基本概念及使用方法,包括元组的创建、元素访问与修改限制、元组间的运算操作等。

Tuple(元组)

元组为一种固定长度、不可改变的Python体序列。

创建方法有:

1. 直接用逗号分开

In [1]: tup = 4, 5, 6
In [2]: tup

Out[2]: (4, 5, 6)

2.创建复杂一些的元组

In [3]: nested_tup = (4, 5, 6), (7, 8)
In [4]: nested_tup
Out
[4]: ((4, 5, 6), (7, 8))

3.将任何序列或迭代子(iterator)转化为元组,用tuple

In [5]: tuple([4, 0, 2])
Out[5]: (4, 0, 2)
In [6]: tup = tuple('string')
In [7]: tup
Out
[7]: ('s', 't', 'r', 'i', 'n', 'g')

通过中括号[ ]来获取元组中的元素

In [8]: tup[0]

Out[8]: 's'

有些元组中的元素是不可改变的

In [9]: tup = tuple(['foo', [1, 2], True])
In [10]: tup[2] = False
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-c7308343b841> in <module>()
----> 1 tup[2] = False
TypeError: 'tuple' object does not support item assignment

如果元组中的一个体是可修改的,如列表list,则可以在内部进行修改

In [11]: tup[1].append(3)
In [12]: tup
Out
[12]: ('foo', [1, 2, 3], True)

可以通过加号“+”来合并元组,得到长元组

In [13]: (4, None, 'foo') + (6, 0) + ('bar',)

Out[13]: (4, None, 'foo', 6, 0, 'bar')

将元组乘以一个整数,则得到整数倍的copy长度的元组

In [14]: ('foo', 'bar') * 4
Out[14]: ('foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar')

这里应注意,体本身没有被复制,而只是增加了索引












这本书主要是用 pandas 连接 SciPy NumPy,用pandas做数据处理是Pycon2012上一个很热门的话题。另一个功能强大的东西是Sage,它将很多开源的软件集成到统一的 Python 接口。, Python for Data Analysis is concerned with the nuts and bolts of manipulating, processing, cleaning, and crunching data in Python. It is also a practical, modern introduction to scientific computing in Python, tailored for data-intensive applications. This is a book about the parts of the Python language and libraries you’ll need to effectively solve a broad set of data analysis problems. This book is not an exposition on analytical methods using Python as the implementation language., Written by Wes McKinney, the main author of the pandas library, this hands-on book is packed with practical cases studies. It’s ideal for analysts new to Python and for Python programmers new to scientific computing., Use the IPython interactive shell as your primary development environment, Learn basic and advanced NumPy (Numerical Python) features, Get started with data analysis tools in the pandas library, Use high-performance tools to load, clean, transform, merge, and reshape data, Create scatter plots and static or interactive visualizations with matplotlib, Apply the pandas groupby facility to slice, dice, and summarize datasets, Measure data by points in time, whether it’s specific instances, fixed periods, or intervals, Learn how to solve problems in web analytics, social sciences, finance, and economics, through detailed examples
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值