元组
序列
字符串、列表和元组都是序列
序列的两个主要特点是索引操作符和切片操作符.
- 索引操作符让我们可以从序列中抓取一个特定的项目。
- 切片操作符让我们能够获取序列的一个切片,即一部分序列。
序列的基本操作:
- len() :求序列长度
In [1]: a = '1234;5lasdfasdf'
In [2]: len(a)
Out[2]: 15
- +:连接2个序列(两个str)
In [3]: a = 'hello'
In [4]: b = 'world'
In [5]: print a+b
helloworld
In [6]:
- *:重复序列元素(*后面根的是int)
In [9]: print a * 3
hellohellohello
In [10]:
- in:判断元素是否在序列中
In [12]: print a
[1, 3, 3, 5]
In [13]: 3 in a
Out[13]: True
- max():返回最大值
In [21]: print a
[1, 3, 3, 5]
In [22]: print max(a)
5
- min():返回最小值
In [21]: print a
[1, 3, 3, 5]
n [23]: print min(a)
1
- cmp(x,y):比较两个序列是否相等
In [24]: a
Out[24]: [1, 3, 3, 5]
In [27]: b = [1,3,3,5]
In [28]: cmp (a,b)
Out[28]: 0
In [29]: cmp(a,'1,3,3,5')
Out[29]: -1
In [30]: cmp(a,[1,3,3,5])
Out[30]: 0
In [31]: cmp(a[1],a[2])
Out[31]: 0
In [32]: cmp(a[0],a[3])
Out[32]: -1
In [33]: cmp(a[3],a[2])
Out[33]: 1
In [34]:
元组:
元组和列表十分相似
元组和字符串一样是不可以变的。
- 元组可以存储一系列的值
- 元组通常用在用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
In [34]: t = (1,3,5,'a',(1,))
In [35]: type(t)
Out[35]: tuple
In [36]: print t
(1, 3, 5, 'a', (1,))
In [37]:
元组操作:
元组和字符串一样属于序列类型,可以通过索引和切片操作
元组值不可变
无组的拆分
t = (1,2,3)
a,b,c=t
In [150]: t = (1, 3, 5, 'a', (1,))
In [151]: print t
(1, 3, 5, 'a', (1,))
In [39]: t[0]
Out[39]: 1
In [40]: print t
(1, 3, 5, 'a', (1,))
In [41]: t[0] = 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-29b3302c4f70> in <module>()
----> 1 t[0] = 2
TypeError: 'tuple' object does not support item assignment
In [42]:
元组中引用变量:
In [42]: a
Out[42]: [1, 3, 3, 5]
In [43]: t = (a,'ab')
In [44]: t
Out[44]: ([1, 3, 3, 5], 'ab')
In [45]:
元组切片:
In [44]: t
Out[44]: ([1, 3, 3, 5], 'ab')
In [45]: first,second = t
In [46]: first
Out[46]: [1, 3, 3, 5]
In [47]: second
Out[47]: 'ab'
In [14]: t
Out[14]: (1, 2, 3)
In [15]: t.index(1)
Out[15]: 0
In [16]: t.index(2)
Out[16]: 1
In [17]: t.index(3)
Out[17]: 2
In [18]: t = (1,3,3,2,5)
In [19]: t.index(5)
Out[19]: 4
In [20]: t.count(3)
Out[20]: 2
In [21]: t.count(5)
Out[21]: 1
列表
列表内可变
列表(list)是处理一组有序项目的数据结构,即要以在列表中存储一个序列的项目。
列表显示是可变类型的数据
创建列表:
list1 = []
list2 = [1,3,3,'a']
list3 = list()
In [22]: list1 = []
In [23]: type(list1)
Out[23]: list
In [24]: list
list list1
In [24]: list2 = list()
In [25]: type(list2)
Out[25]: list
In [26]: list
list list1 list2
In [26]: list2
Out[26]: []
In [27]: list
list list1 list2
In [27]: list3 = ['a',1,2,(1,),[1,'a']]
In [28]: list3
Out[28]: ['a', 1, 2, (1,), [1, 'a']]
In [29]: len(list3)
Out[29]: 5
In [30]:
In [30]: list3[-1]
Out[30]: [1, 'a']
In [31]: list3[0]
Out[31]: 'a'
In [37]: list3
Out[37]: ['a', 1, 2, (1,), [1, 'a']]
In [38]: list3[0:3]
Out[38]: ['a', 1, 2]
In [40]: list3[0] = 'b'
In [41]: list3
Out[41]: ['b', 1, 2, (1,), [1, 'a']]
列表操作:
取值:
切片和索引
添加:(append()默认加到最后一个元素)
list.append()
In [85]: list3
Out[85]: ['b', [], (1,), 1]
In [86]: list3[1]
Out[86]: []
In [87]: list3[1].append('abc')
In [88]: list3
Out[88]: ['b', ['abc'], (1,), 1]
追加
In [42]: list3.append(333)
In [43]: list3
Out[43]: ['b', 1, 2, (1,), [1, 'a'], 333]
In [44]:
In [43]: list3
Out[43]: ['b', 1, 2, (1,), [1, 'a'], 333]
In [44]: list2
Out[44]: []
In [45]: list2 = [1111,222,333]
In [46]: list2 + list3
Out[46]: [1111, 222, 333, 'b', 1, 2, (1,), [1, 'a'], 333]
In [47]:
In [52]: (list2 + list3)*2
Out[52]:
[1111,
222,
333,
'b',
1,
2,
(1,),
[1, 'a'],
333,
1111,
222,
333,
'b',
1,
2,
(1,),
[1, 'a'],
333]
In [53]:
删除
del list[]
list.remove(list[])
In [53]: list2
Out[53]: [1111, 222, 333]
In [54]: del list
list list1 list2 list3
In [54]: del list2[2]
In [55]: list2
Out[55]: [1111, 222]
In [55]: list2
Out[55]: [1111, 222]
In [56]: list2.remove(list2[1])
In [57]: list2
Out[57]: [1111]
修改:
list[] = x
In [74]: list2
Out[74]: [1111, 22222, 2223, 22233434, 33434]
In [75]: list2[0] = 0000
In [76]: list2
Out[76]: [0, 22222, 2223, 22233434, 33434]
In [77]: list2
Out[77]: [0, 22222, 2223, 22233434, 33434]
In [78]: list2.insert(1,66666)
In [79]: list2
Out[79]: [0, 66666, 22222, 2223, 22233434, 33434]
In [80]:
查找:
var in list
In [57]: list2
Out[57]: [1111]
In [58]: 1 in list2
Out[58]: False
In [59]: 1111 in list2
Out[59]: True
In [60]:
排序:
In [90]: list3
Out[90]: ['b', ['abc'], (1,), 1]
In [91]: list3.sort()
In [92]: list3
Out[92]: [1, ['abc'], 'b', (1,)]
In [93]:
反转:
In [93]: list3
Out[93]: [1, ['abc'], 'b', (1,)]
In [94]: list3.reverse()
In [95]: list3
Out[95]: [(1,), 'b', ['abc'], 1]
In [96]:
POP默认删除最后一个,pop删除对应位置.
In [103]: list2
Out[103]: [0, 66666, 22222, 2223, 22233434, 33434]
In [104]: list2.pop(1)
Out[104]: 66666
In [105]: list2
Out[105]: [0, 22222, 2223, 22233434, 33434]
In [106]: list2.pop()
Out[106]: 33434
In [107]: list2.pop()
Out[107]: 22233434
In [108]: list2
Out[108]: [0, 22222, 2223]
In [109]:
迭代:
字符串,元组都是可迭代的,都可以通过for循环去访问。
In [120]: list3
Out[120]: ['b', ['abc']]
In [121]: range(5)
Out[121]: [0, 1, 2, 3, 4]
In [122]: list3.extend(range(5))
In [123]: list3
Out[123]: ['b', ['abc'], 0, 1, 2, 3, 4]
In [125]: list3.extend('5555')
In [126]: list3
Out[126]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5']
In [127]: list3.extend('a')
In [128]: list3
Out[128]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5', 'a']
In [129]: list3.extend('1234')
In [130]: list3
Out[130]: ['b', ['abc'], 0, 1, 2, 3, 4, '5', '5', '5', '5', 'a', '1', '2', '3', '4']
In [131]:
tab补齐:
python 一切都是对象
帮助:
help(t.index)
Help on built-in function index:
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
(END)
异常:
练习:
现有列表
list1 = ['XXXX', 'b', 3, 'c', 3, '&', 'a', 3, '3', 3, 'aa', '3', 'XXXX']
list2 = ['e', 'f', 'g']
要求对其做以下操作:
1. 取出 ‘XXXX’ 中间的部分,形成一个新的列表list3
In [8]: list1 = ['XXXX', 'b', 3, 'c', 3, '&', 'a', 3, '3', 3, 'aa', '3', 'XXXX']
In [9]: list3 = list1[1:-1]
In [10]: print list3
['b', 3, 'c', 3, '&', 'a', 3, '3', 3, 'aa', '3']
In [11]:
2. 对list3 做一下几部操作
1)删除特殊符号
In [13]: del list3[4]
In [14]: print list3
['b', 3, 'c', 3, 'a', 3, '3', 3, 'aa', '3']
In [15]:
2)统计 3 在list3中出现的次数
In [17]: list3.count(3)
Out[17]: 4
In [18]:
3)用最简短的代码去除list3中 26个字母以外的元素(要求只能对list3操作)
list3 = list3[0:5:2]
4)对list3排序
In [28]: list3.sort()
In [29]: print list3
Out[29]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c']
In [30]:
5)在末尾追加'd',并把list2追加到list3
In [30]: list3.append('d')
In [31]: print list3
Out[31]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd']
In [32]:
In [46]: list2
Out[46]: ['e', 'f', 'g']
In [48]: list3.append(list2)
In [49]: print list3
Out[49]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
3. 现有两个变量
a = ('h',)
b = ('h')
1)将a和b分别追加到上一题的list3中,观察有什么区别
In [50]: a = ('h',)
In [51]: b = ('h')
In [52]: print list3
Out[52]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
In [53]: list3.append(a)
In [54]: print list3
Out[54]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], ('h',)]
In [55]: del list3[-1]
In [56]: print list3
Out[56]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g']]
In [57]: list3.append(b)
In [58]: print list3
Out[58]: [3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], 'h']
In [59]:
2)将1生成的list3转换成元组(扩展:自己搜索方法)
In [73]: t = (list3,)
In [74]: type(t)
Out[74]: tuple
In [75]: print t
([3, 3, 3, 3, '3', '3', 'a', 'aa', 'b', 'c', 'd', ['e', 'f', 'g'], 'h'],)
3)打印出只有一个元素'h'的元组,在2中生成的元组中的索引
print tuple(list3).index(h)