python_day3.2

一:列表

1.列表里的元素可以有很多类型,比如整数,浮点数,字符串,对象。

2.创建列表:创建一个普通的列表;创建一个混合的列表;创建一个空的列表。

>>> member=['张三','李四','王五']
>>> number=[1,2,3,4]
>>> mix=[1,'lala',3.14,[2,1,4]]
>>> empty=[]
>>> member
['张三', '李四', '王五']
>>> mix
[1, 'lala', 3.14, [2, 1, 4]]
>>> 
View Code

3.向列表中添加元素

append函数:可以添加一个元素,不能添加多个

>>> member.append('赵六')
>>> member
['张三', '李四', '王五', '赵六']
>>> len(member)
4
>>> member.append('aa','bb')
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    member.append('aa','bb')
TypeError: append() takes exactly one argument (2 given)
View Code

extend函数:可以添加多个元素,多个元素用中括号

>>> member.extend('aa','bb')
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    member.extend('aa','bb')
TypeError: extend() takes exactly one argument (2 given)
>>> member.extend(['aa','bb'])
>>> member
['张三', '李四', '王五', '赵六', 'aa', 'bb']
View Code

insert函数:可以在固定位置添加一个元素

>>> member.insert(0,'wwq')
>>> mmeber
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    mmeber
NameError: name 'mmeber' is not defined
>>> member
['wwq', '张三', '李四', '王五', '赵六', 'aa', 'bb']
View Code

4.从列表中获取元素

>>> member[1]
'张三'

5.从列表中删除元素,三种方法:remove(),del, pop()

>>> member.remove('wwq')
>>> member
['张三', '李四', '王五', '赵六', 'aa', 'bb']
>>> del member[1]
>>> member
['张三', '王五', '赵六', 'aa', 'bb']
>>> member.pop()
'bb'
>>> member
['张三', '王五', '赵六', 'aa']
>>> name=member.pop()
>>> name
'aa'
>>> member.pop(1)
'王五'
>>> member
['张三', '赵六']
>>> del member
>>> membr
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    membr
NameError: name 'membr' is not defined
>>> member
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    member
NameError: name 'member' is not defined
>>> 
View Code

6.分片slice

利用索引值每次可以从列表中取出一个元素,如果要一次性取出多个元素要利用slice

member[1:3] 从1开始,不包括3;是一种拷贝,原列表不发生变化;可以用来拷贝原列表。

>>> member=['aa','bb','cc','dd']
>>> member[1:3]
['bb', 'cc']
>>> member[:2]
['aa', 'bb']
>>> member2=member[:]
>>> member2
['aa', 'bb', 'cc', 'dd']
>>> 
View Code

7.列表的一些常用的操作符:比较,逻辑,连接,重复,成员关系操作符

>>> list1=[123]
>>> list2=[234]
>>> list1>list2
False
>>> list1=[123,789]
>>> list2=[234,90]
>>> list1>list2
False
>>> list3=list1+list2
>>> list3
[123, 789, 234, 90]
View Code

list1+'wwq'是不可以的

>>> list1*3
[123, 789, 123, 789, 123, 789]
>>> list1*=3
>>> list1
[123, 789, 123, 789, 123, 789]
>>> 123 in list1
True
>>> 123 not in list1
False
>>> list2=[123,['aa','bb']]
>>> aa in list2
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    aa in list2
NameError: name 'aa' is not defined
>>> 'aa'in list2
False
>>> 'aa'in list2[1]
True

8.列表的内置函数

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> list1.count(123)
3
>>> list1.index(123)
0
>>> list1.index(123,2)
2
>>> list1.reverse()
>>> list1
[789, 123, 789, 123, 789, 123]
>>> list2=[1,4,2,90,7]
>>> list2.sort()
>>> list2
[1, 2, 4, 7, 90]
 
>>> list2.sort(reverse=True)
>>> list2
[90, 7, 4, 2, 1]
>>> 

9.拷贝

>>> list2
[90, 7, 4, 2, 1]
>>> list3=list2
>>> list2
[90, 7, 4, 2, 1]
>>> list4=list2[:]
>>> list4
[90, 7, 4, 2, 1]
>>> list2.sort()
>>> list2
[1, 2, 4, 7, 90]
>>> list3
[1, 2, 4, 7, 90]
>>> list4
[90, 7, 4, 2, 1]
View Code

10.元组

元组和列表在实际使用中是十分相似的;但是列表可以任意修改一个元素,元组不可以(不能改变,删除,插入,翻转等)。

创建和访问元组:

>>> tuple1=(1,2,3,5)
>>> tuple1
(1, 2, 3, 5)

访问元组中某一个元素:

>>> tuple1[1]
2

拷贝:
>>> tuple2=tuple1[:]
>>> tuple2
(1, 2, 3, 5)

给元组中某一个元素赋值,比如tuple1[1]=9是不可以的。

创建元组:tuple1=2,3,4 或者tuple1=1, 或者tuple1=(1,) ,创建空元组tuple1=()。

tuple1=(1)是int类型,不是一个元组。

在元组中添加元素的方法:

>>> temp=('张三','李四','王五')
>>> temp=temp[:2]+('wwq',)+temp[2:]
>>> temp
('张三', '李四', 'wwq', '王五')
>>> 

 

posted @ 2019-04-05 19:59  wwq1204  阅读(134)  评论(0编辑  收藏  举报