一、list和tuple的区别
最根本的区别,list是可变数据类型,tuple是不可变数据类型
list用的是[],tuple用的是().
tuple没有insert, pop,append方法
因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list[0:10:2])
#输出的结果为
[0, 2, 4, 6, 8]
tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print(tuple[1:10:2])
#输出的结果为
(1, 3, 5, 7, 9)
二、学到的序列有哪几种
python序列类型包括:列表、元组、字典
三、list中所有方法的使用
1.append(self, object, /)
append() 方法用于在列表末尾添加新的对象。
aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList;
#输出结果为
Updated List : [123, 'xyz', 'zara', 'abc', 2009]
2.clear(self, /)
clear() 函数用于清空列表,类似于 del a[:]。
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.clear()
print ("列表清空后 : ", list1)
#输出结果为
列表清空后 : []
3.copy(self, /)
copy() 函数用于复制列表,类似于 a[:]。
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list2 = list1.copy()
print ("list2 列表: ", list2)
#输出结果为
list2 列表: ['Google', 'Runoob', 'Taobao', 'Baidu']
4.count(self, value, /)
count() 方法用于统计某个元素在列表中出现的次数。
List = [123, 'xyz', 'zara', 'abc', 123]
print("Count for 123 : ", List.count(123))
print("Count for zara : ", List.count('zara'))
#输出结果为
Count for 123 : 2
Count for zara : 1
5.extend(self, iterable, /)
extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。
list1 = [123, 'xyz', 'zara', 'abc', 123]
list2 = [2009, 'manni']
list1.extend(list2)
print ("extended list : ", list1)
#输出结果为
extended list : [123, 'xyz', 'zara', 'abc', 123, 2009, 'manni']
6.index(self, value, start=0, stop=9223372036854775807, /)
index() 函数用于从列表中找出某个值第一个匹配项的索引位置。
list = [123, 'xyz', 'runoob', 'abc']
print ("xyz 索引位置: ", list.index( 'xyz' ))
print ("runoob 索引位置 : ", list.index( 'runoob', 1, 3 ))
#输出结果为
xyz 索引位置: 1
runoob 索引位置 : 2
7.insert(self, index, object, /)
insert() 函数用于将指定对象插入列表的指定位置。
list = [123, 'xyz', 'zara', 'abc']
list.insert( 3, 2009)
print("Final list : ", list)
#输出结果为
Final list : [123, 'xyz', 'zara', 2009, 'abc']
8.pop(self, index=-1, /)
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
list1 = ['Google', 'Runoob', 'Taobao']
list_pop=list1.pop(1)
print("删除的项为 :", list_pop)
print("列表现在为 : ", list1)
#输出的结果为
删除的项为 : Runoob
列表现在为 : ['Google', 'Taobao']
9.remove(self, value, /)
remove() 函数用于移除列表中某个值的第一个匹配项。
list = [123, 'xyz', 'zara', 'abc', 'xyz']
list.remove('xyz')
print("list1 : ", aList)
list.remove('abc')
print("list2: ", list)
#输出结果为
list1 : [123, 'xyz', 'zara', 'abc', 123]
list2: [123, 'zara', 'xyz']
10.sort(self, /, *, key=None, reverse=False)
sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
list = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook']
list.sort()
print("list : ", list)
#输出结果为
list : ['123', 'Facebook', 'Google', 'Runoob', 'Taobao']
四、将元组和列表转换成字典
list = [('1',1),('2','jiqw'),('3',1372)]
dict(list)
#输出结果为
{'1': 1, '3': 1372, '2': 'jiqw'}
五、dict中所有方法的使用
1.clear(…)
Python 字典(Dictionary) clear() 函数用于删除字典内所有元素。
dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" % len(dict)
dict.clear()
print "End Len : %d" % len(dict)
#输出结果为
Start Len : 2
End Len : 0
2.copy(…)
Python 字典(Dictionary) copy() 函数返回一个字典的浅复制。
dict1 = {'Name': 'Zara', 'Age': 7};
dict2 = dict1.copy()
print("New Dictinary : %s" % str(dict2))
#输出结果为
New Dictinary : {'Age': 7, 'Name': 'Zara'}
3.get(self, key, default=None, /)
Python 字典(Dictionary) get() 函数返回指定键的值。
dict = {'Name': 'Runoob', 'Age': 27}
print("Age : %s" % dict.get('Age'))
print("Sex : %s" % dict.get('Sex'))
print('Salary: %s' % dict.get('Salary', 0.0))
#输出结果为
Age : 27
Sex : None
Salary: 0.0
4.items(…)
Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。
dict = {'Google': 'www.google.com', 'Baidu': 'www.baidu.com', 'taobao': 'www.taobao.com'}
print("字典值 : %s" % dict.items())
for key, values in dict.items():
print(key,values)
#输出结果为
字典值 : dict_items([('Google', 'www.google.com'), ('Baidu', 'www.baidu.com'), ('taobao', 'www.taobao.com')])
Google www.google.com
Runoob www.baidu.com
taobao www.taobao.com
5.pop(…)
Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值。
site= {'name': 'lihua', 'alexa': 10000, 'url': 'www.baidu.com'}
element = site.pop('name')
print('删除的元素为:')
print(element)
print('字典为:')
print(site)
#输出的结果为
删除的元素为:
lihua
字典为:
{'alexa': 10000, 'url': 'www.baidu.com'}
六、list或dict浅拷贝画图加代码解释
1.浅拷贝
浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象。
a = {1: [1,2,3]}
b = a.copy()
a, b
({1: [1, 2, 3]}, {1: [1, 2, 3]})
a[1].append(4)
a, b
({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
2.深拷贝
深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象。
import copy
c = copy.deepcopy(a)
a, c
({1: [1, 2, 3, 4]}, {1: [1, 2, 3, 4]})
a[1].append(5)
a, c
({1: [1, 2, 3, 4, 5]}, {1: [1, 2, 3, 4]})
“”"