Python中的List,Tuple和Dictionary

本文深入探讨了Python中的三种重要数据结构——List、Tuple和Dictionary。List部分详细介绍了读取元素、计算长度、操作符、切片以及其可变性。Tuples特性聚焦在不可变性和随机数应用。而Dictionary部分则讨论了相关方法和长整型long的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

List


List是一组有序的元素,和String有些类似,只是String中只能是字符,而List中则可以包含任何类型的元素,如下面的例子所示:
[10, 20, 30, 40] 
["spam", "bungee", "swallow"] 
["hello", 2.0, 5, [10, 20]] 
读取元素:
List的index可以是任意的整型表达式,若为负数,则总后向前数,如下面的这些例子:
>>> numbers[3-2] 
5 
>>> numbers[-1] 
5 
List的长度:
使用len()函数,可以方便地获得list的长度:
horsemen = ["war", "famine", "pestilence", "death"] 

i = 0 
while i < len(horsemen): 
  print horsemen[i] 
  i = i + 1 

List中不同类型的元素都被计数为1,例如下面的这个list,长度为4
['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]] 

List元素的判别:
使用in关键字可以方便的判断一个元素是否在List中,如:
>>> horsemen = ['war', 'famine', 'pestilence', 'death'] 
>>> 'pestilence' in horsemen 
True 
>>> 'debauchery' in horsemen 
False 

>>> 'debauchery' not in horsemen 
True
List的操作符:
使用“+”操作符可以连接两个List:
>>> a = [1, 2, 3] 
>>> b = [4, 5, 6] 
>>> c = a + b 
>>> print c 
[1, 2, 3, 4, 5, 6] 
使用"*"可以快速复制List中的元素
>>> [0] * 4 
[0, 0, 0, 0] 
>>> [1, 2, 3] * 3 
[1, 2, 3, 1, 2, 3, 1, 2, 3] 
List切片:
可以使用指定上下限的方式来获得一个List的Slice:
>>> list = ['a', 'b', 'c', 'd', 'e', 'f'] 
>>> list[1:3] 
['b', 'c'] 
>>> list[:4] 
['a', 'b', 'c', 'd'] 
>>> list[3:] 
['d', 'e', 'f'] 
List是可变的
与Python中的数组不同,List中元素的值是可以修改的:
>>> fruit = ["banana", "apple", "quince"] 
>>> fruit[0] = "pear" 
>>> fruit[-1] = "orange" 
>>> print fruit 
['pear', 'apple', 'orange'] 
使用Splice的方式可以批量增加、删除或修改List中的元素:
修改
>>> list = ['a', 'b', 'c', 'd', 'e', 'f'] 
>>> list[1:3] = ['x', 'y'] 
>>> print list 
['a', 'x', 'y', 'd', 'e', 'f'] 
删除
>>> list = ['a', 'b', 'c', 'd', 'e', 'f'] 
>>> list[1:3] = [] 
>>> print list 
['a', 'd', 'e', 'f'] 
增加
>>> list = ['a', 'd', 'f'] 
>>> list[1:1] = ['b', 'c'] 
>>> print list 
['a', 'b', 'c', 'd', 'f'] 
>>> list[4:4] = ['e'] 
>>> print list 
['a', 'b', 'c', 'd', 'e', 'f'] 
还可以使用del函数删除List中的元素:
>>> a = ['one', 'two', 'three'] 
>>> del a[1] 
>>> a 
['one', 'three'] 
del删除多个元素:
>>> list = ['a', 'b', 'c', 'd', 'e', 'f'] 
>>> del list[1:5] 
>>> print list 
['a', 'f'] 
Objects和Values
Python中每个对象都有自己的id,可以使用id()函数来查看。
例如下面这段代码:
a = "banana" 
b = "banana"
print id(a)
print id(b)
输出类似:
162053664
162053664
这说明对于String常量,他们的name都会binding到同一个对象上。同样是immutable的数组,会不会也是这样?我们可以用下面这段代码来测试:
a = (1, 2, 3)
b = (1, 2, 3)
print id(a)
print id(b)
我的本地输出:
162055028
162055108
对于List:
a = [1, 2, 3]
b = [1, 2, 3]
print id(a)
print id(b)
我的本地输出:
160416428
162000588

从上面的输出可以看出,immutable的数组和mutable的列表都不会binding到同一个对象上,而且同时声明的数组id距离不大,而类表id却相差较大,这也许与他们在内存中的位置有关。
Strings和Lists
String中最常用的两个方法都与List相关。split方法将String分解为字符串List。split()方法的参数有两个,一个是要被分解的String,另一个是分割符。如下面代码所示:
>>> import string 
>>> song = "The rain in Spain..." 
>>> string.split(song) 
['The', 'rain', 'in', 'Spain...'] 
在上面的代码中,split方法只传入了要被分解的String一个参数,此时Split将使用默认分割符:空格
下面是传入分割符的示例代码:
>>> string.split(song, 'ai') 
['The r', 'n in Sp', 'n...'] 
和split对应的一个方法为join(),用起来也与split类似,下面是join的例子:
>>> list = ['The', 'rain', 'in', 'Spain...'] 
>>> string.join(list) 
'The rain in Spain...' 
传入分割符的例子:
>>> string.join(list, '_') 
'The_rain_in_Spain...'

Tuples

Mutability and Tuples
和String相同的是,Python中的数组也是immutable的。这与Java、C++等很不一样。Tuple可以这样创建:
tuple = 'a', 'b', 'c', 'd', 'e' 
也可以这样创建:
tuple = ('a', 'b', 'c', 'd', 'e') 
若要创建只含一个元素的Tuple,需要添加一个“逗号在”后面,如下所示:
t1 = ('a',) 
type(t1) 
<type 'tuple'>
否则,则t1将binding到一个String上,如下所示:
t2 = ('a') 
type(t2) 
<type 'str'> 
与List类似,可以使用指定上下区间的方式来提取一个新的Tuple,也可以使用“+”来生成一个新的Tuple,如下所示:
>>> tuple = ('a', 'b', 'c', 'd', 'e') 
>>> tuple[1:3] 
('b', 'c') 
>>> tuple = ('A',) + tuple[1:] 
>>> tuple 
('A', 'b', 'c', 'd', 'e') 
随机数
使用Python中的random库中的random函数可以获得一个0到1之间的float随机数,下面的代码将生成10个随机数:
import random

for i in range(10):
    x = random.random()
    print type(x)

Dictionary

Python中的Dictionary可以使用任意immutable的类型做index。创建Dictionary的一种常用方式是首先创建一个空的Dictionary,然后逐渐向其中添加元素:
>>> eng2sp = {} 
>>> eng2sp['one'] = 'uno' 
>>> eng2sp['two'] = 'dos' 
打印效果如下:
>>> print eng2sp 
{'one': 'uno', 'two': 'dos'} 
还可以用这种方式创建一个空的Dictionary:
>>> eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} 
Dictionary方法
获得Dictionary中的keys:
>>> eng2sp.keys() 
['one', 'three', 'two'] 
获得Dictionary中的values:
>>> eng2sp.values() 
['uno', 'tres', 'dos']
判断Dictionary中是否包含某个key:
>>> eng2sp.has_key('one') 
True 
>>> eng2sp.has_key('deux') 
False 
获得Dictionary的copy:
>>> opposites = {'up': 'down', 'right': 'wrong', 'true': 'false'} 
>>> copy = opposites.copy() 
长整型 long
Python中的"long"可以表示任意长度的整数,获得long类型有两种方法,一种是在数字后面加“L”,另一种是使用long()函数,如下所示:
>>> type(1L) 
<type 'long'>
>>> long(1) 
1L 
>>> long(3.9) 
3L 
>>> long('57') 
57L 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值