Python学习笔记 之元组、列表及字典

1. 元组 —— 不可更改的数据序列


元组是值的序列,其中的每个值都可以被单独访问,元组是Python的基本类型。在创建时可以被识别,元组创建使用圆括号包围值。

例如:
filler = ("string", "filled", "by a", "tuple");

访问元组中的单个值,使用解引用来直接访问。在元组后面放置方括号,并且从0起,计算要引用的元素的位置。
因此元组中,第一个元素的位置为0, 第二个元素的位置为1,依次类推。

例如:
>>> a = ("first", "second", "three");
>>> print("The first element of the tuple is %s" %a[0]);
The first element of the tuple is first
>>> print("The second element of the tuple is %s" %a[1]);
The second element of the tuple is second
>>> print("The third element of the tuple is %s" %a[2]);
The third element of the tuple is three


len() 方法返回元组的长度。

嵌套元组:
元组的一个元素可以是对另外一个元组的引用:
例如:
>>> a = ("first", "second", "three");
>>> b = (a, "b's second element");
>>> print(b);
(('first', 'second', 'three'), "b's second element")

使用解引用访问:
>>> print(b[0][0]);
first
>>> print(b[0][1]);
second
>>> print(b[0][2]);
three
>>> print(b[1]);
b's second element

>>> first_tuple = b[0];
>>> print(first_tuple[0]);
first
>>> print(first_tuple[1]);
second
>>> print(first_tuple[2]);
three

元组可以包含各种类型,但是一旦创建之后,它就不可变了。试图修改元组的元素值将发生错误。

2. 列表 —— 可以更改的数据序列

列表如同元组,包含从0开始引用的元素的序列,列表用方括号创建:
例如:
>>> breadfast = ["coffee", "tea", "toast", "egg"];
>>> print(breadfast);
['coffee', 'tea', 'toast', 'egg']

列表也使用解引用的方法引用列表中的元素。
例如:
>>> print(breadfast);
['coffee', 'tea', 'toast', 'egg']
>>> count = 0;
>>> print("Today's breakfast is %s" %breadfast[count]);
Today's breakfast is coffee
>>> count = 1
>>> print("Today's breakfast is %s" %breadfast[count]);
Today's breakfast is tea
>>> count = 2;
>>> print("Today's breakfast is %s" %breadfast[count]);
Today's breakfast is toast
>>> count = 3;
>>> print("Today's breakfast is %s" %breadfast[count]);
Today's breakfast is egg

列表是可以被随时修改的。
例如:
>>> breadfast[count] = "sausages";
>>> print("Today's breakfast is %s" %breadfast[count]);
Today's breakfast is sausages

同时:
len()方法获取列表长度, append()方法向列表末尾添加元素,
例如:
>>> breadfast.append("waffles");
>>> print(breadfast);
['coffee', 'tea', 'toast', 'sausages', 'waffles']

一次向列表中加入多个元素,可以使用extend()方法,例如:
例如:
>>> breadfast.extend(["juice", "decaf", "oatmeal"]);
>>> print(breadfast);
['coffee', 'tea', 'toast', 'sausages', 'waffles', 'juice', 'decaf', 'oatmeal']


3. 字符串


字符串可以像列表一样被处理:
例如:
>>> last_names = ["Douglass", "Jefferson", "Williams", "Frank"];
>>> print("%s" %last_names[0]);
Douglass
>>> print("%s" %last_names[0][0]);
D
>>> print("%c" %last_names[0][0]);
D

列表的一些共有属性:
元组和列表是两种类型的序列,字符串也可看作序列。


引用最后一个元素:
一般的方法:
>>> last_elem = len(last_names) -1;
>>> print("%s" %last_names[last_elem]);
Frank

Python特有方法:
可以使用数值 -1 访问一个序列最后的一个元素,-2访问倒数第二个元素。
>>> print(last_names[-1]);
Frank



序列范围:
可以获取序列的一部分,也即将序列分片,创建一个单独使用的副本。
从列表中创建的分片为一个列表,从元组中创建的片段为一个元组,字符串的片段为字符串。

使用[start:end]进行分片:
例如:
>>> slice_me = ("The", "next", "time", "we", "meet", "drinks", "are", "on", "me");
>>> sliceed_tuple = slice_me[5:9];
>>> print(sliceed_tuple);
('drinks', 'are', 'on', 'me')

>>> slice_list = ["The", "next", "time", "we", "meet", "drinks", "are", "on", "me"];
>>> sliced_list = slice_list[5:9];
>>> print(sliced_list);
['drinks', 'are', 'on', 'me']

>>> slice_str = "The next time we meet, drinks are on me";
>>> sliced_str = slice_str[5:9];
>>> print(sliced_str);
ext 


通过附加序列增长列表
使用append()将序列加入一个序列中,则是当作一个元素加入,增加一个分层的序列。
>>> living_room = ( "rug", "table", "chair", "TV", "dustbin");
>>> apartment = [];
>>> apartment.append(living_room);
>>> apartment
[('rug', 'table', 'chair', 'TV', 'dustbin')]


如果要复制一个序列的所有元素,可以使用extend()方法。
>>> apartment = [];
>>> apartment.extend(living_room);
>>> apartment
['rug', 'table', 'chair', 'TV', 'dustbin']



使用列表临时存储数据
将要处理的数据临时放入列表中,可以按照数据进入的顺序进行处理。
为了防止列表变得笨重,可以使用pop方法在处理完列表的一个数据之后,将其从引用列表中删除。
×××当删除引用之后,它原来在列表中占据的位置会填上后续的元素,列表减少的元素个数等于已经弹出的元素个数。
>>> todays_temp = [20, 32, 33, 31];
>>> todays_temp
[20, 32, 33, 31]
>>> todays_temp.append(29);
>>> todays_temp
[20, 32, 33, 31, 29]
>>> morning = todays_temp.pop(0);
>>> morning
20
>>> todays_temp
[32, 33, 31, 29]
>>> late_morning = todays_temp.pop(0); // pop方法删除并返回指定下标的值
>>> late_morning
32


4. 字典 —— 以名称索引的分组数据

字典类似于列表和元组,包含一组数据的另外一种容器。元组和列表以数字索引,字典却是用名称索引。
名称可以是字母,数据,字符串,或者符号。

>>> menus_specials = {};
>>> menus_specials["breakfast"] = "canadian ham";
>>> menus_specials["lunch"] = "tuna surprize";
>>> print(menus_specials);
{'lunch': 'tuna surprize', 'breakfast': 'canadian ham'}

字典用花括号创建,刚开始,可以创建最简单的字典,即空字典,通过逐行指定名称和值对它进行初始化。
字典中索引和值都有特殊的名称:字典中索引的名称为键,对应的值叫做值。
创建一个完整的字典,在花括号中,指定每一个键以及它对应的值,并以冒号分隔它们。

例如:
menus_specials = {'lunch': 'tuna surprize', 'breakfast': 'canadian ham'};

从字典中获取键和值:
例如:
键:
>>> hungry = menus_specials.keys();
>>> print(list(hungry));
['lunch', 'breakfast']
值:
>>> starving = menus_specials.values();
>>> print(list(starving));
['tuna surprize', 'canadian ham']

字典的工作原理是:每个键都是不同的,不可以有完全相同的两个键,但是可以有多个重复的值。

字典也可以比较:
像列表一样,一个字典中的每一个键与值必须与另外一个字典中的键与值一一对应,两个字典的键与值必须一一对应相等。

By Andy

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值