python基础知识(1)——列表和元组

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

序列概览

在python中,最基本的数据结构是序列(sequence)。序列中的每个元素被分配一个序列号–即元素的位置,也称索引。第一个索引是0,第二个是1。
列表和元组是python最常用的两种类型。列表和元组的主要区别在于,列表可以修改,元组则不能。

通用序列操作

索引(index)、分片(slicing)、加(adding)、乘(multiplying)、以及检查某个元素是否属于序列的成员(成员资格)

索引

序列中的所有元素都是有编号的-从0开始递增,最后一个元素位置编号是-1

>>>greeting = 'hello'
>>>greeting[0]
'H'
>>>greeting[-1]
'o'
分片

与使用索引来访问单个元素类似,可以分片来访问一定范围内的元素。

>>>numbers = range(1,10)
>>>numbers[3:6]
[4,5,6]
>>>numbers[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>numbers[-3:]
[7,8,9]
序列相加

通过加运算符可以进行序列的连接操作:

>>>[1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]
乘法
>>> 'python '*4
'python python python python '

[]表示空列表

成员资格

为了检查一个值是否在序列中,用in运算符

>>> permissions = 'rw'
>>> 'w' in permissions
True
长度、最小值和最大值

内建函数len、min和max

列表:Python的’苦力‘

list函数:

>>>list('Hello')
['H', 'e', 'l', 'l', 'o']
基本的列表操作

1.改变列表:元素赋值

>>>x = [1,1,1]
>>>x[1] = 2
>>>x
[1,2,1]

2.删除元素

>>> names = ['Alice', 'Beth', 'Ceil']
>>> del names[1]
>>> names
['Alice', 'Ceil']

3.分片赋值

>>> name = list('perl')
>>> name[1:]  = list('ython')
>>> name
['p', 'y', 't', 'h', 'o', 'n']
列表方法

1.append
用于列表末尾追加新的对象

>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

2.count
count方法统计某个元素在列表中出现的次数

>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2

3.extend
extend方法可以在列表的末尾一次性追加另一个序列中的多个值。

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a + b
[1, 2, 3, 4, 5, 6]

4.index
index方法用于列表中找出某个值第一个匹配项的索引位置:

>>> knights = ['we', 'are', 'the', 'knights', 'who', 'say', 'ni']
>>> knights.index('who')
4

5.insert
insert方法用于将对象插入到列表中:

>>> numbers = range(1,8)
>>> numbers.insert(3, 'four')
>>> numbers
[1, 2, 3, 'four', 4, 5, 6, 7]

6.pop
pop方法会移除列表中的一个元素(默认最后一个),并返回该元素的值。

>>> x = [1, 2, 3]
>>> x.pop()
3
>>> x.pop(1)
2

7.remove
remove方法用于移除列表中某个值的第一个匹配项:

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']

8.reverse
reverse方法将列表中的元素反向存放

>>> x = [1, 2, 3]
>>> x.reverse()
>>> x
[3, 2, 1]

9.sort
排序

>>> x = [4, 6, 2, 1, 7 ,0]
>>> x.sort()
>>> x
[0, 1, 2, 4, 6, 7]
>>> y = x.sort()
>>> y
None
>>> y = sorted(x)
>>> y
[0, 1, 2, 4, 6, 7]

10.cmp

>>>cmp(42, 32)
1
>>>cmp(88,100)
-1
>>>cmp(10,10)
0

元组:不可变序列

如果你用逗号分隔了一些值,那么你就自动创建了元组。元组是通过圆括号括起来的。

>>> 1,2,3
(1, 2, 3)
>>> (1,2,3)
(1, 2, 3)
>>> 42
42
>>> 42,
(42,)
>>> 3*(40+2)
126
>>> 3*(40+2,)
(42, 42, 42)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值