标签:
列表和元组的主要区别在于,列表是可以修改的,元组则不能。说白了就是要求添加元素,那么就选择列表;不能修改则选择元组。
如:
l1 = [‘alex‘,18]
l2 = [[‘alex‘,18],[‘eric‘,19]]
num = [1,2,3,4,5,6,7,8,9,10]
>>> l1[-1]
18
>>> l1[0]
‘alex‘
>>> l2[1]
[‘eric‘, 19]
>>>
>>> l2[0:1]
[[‘alex‘, 18]]
>>> num[:3]
[1, 2, 3]
>>> num[-4:]
[7, 8, 9, 10]
>>> num[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> num[3:-4]
[4, 5, 6]
>>>
>>> l1[1] = 16
>>> l1
[‘alex‘, 16]
>>> del l1[1]
>>> l1
[‘alex‘]
>>>
>>> l1.append(18)
>>> l1
[‘alex‘, 18]
>>> l1.append(‘IT‘)
>>> l1
[‘alex‘, 18, ‘IT‘]
>>> l3 = [12,12,243,13,13,34,35]
>>> l3.count(13)
2
>>> l1.extend(l3)
>>> l1
[‘alex‘, 18, ‘IT‘, 12, 12, 243, 13, 13, 34, 35]
>>> l3.extend([110,120])
>>> l3
[12, 12, 243, 13, 13, 34, 35, 110, 120]
>>>
该方法用于从列表中找出某个值第一次被匹配到的索引位置
>>> l3.index(34)
5
>>>
该方法用于将对象插入到列表中
>>> num.insert(2,‘four‘)
>>> num
[1, 2, ‘four‘, 3, 4, 5, 6, 7, 8, 9, 10]
该方法会移除列表中的一个元素
>>> num.pop()
10
>>> num
[1, 2, ‘four‘, 3, 4, 5, 6, 7, 8, 9]
>>> num.pop(2)
‘four‘
>>> num
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
该方法用于移出列表中某个值的第一个匹配项
>>> num.insert(5,3)
>>> num
[1, 2, 3, 4, 5, 3, 6, 7, 8, 9]
>>> num.remove(3)
>>> num
[1, 2, 4, 5, 3, 6, 7, 8, 9]
>>>
该方法用于列表中的元素反向存放
>>> num.reverse()
>>> num
[9, 8, 7, 6, 3, 5, 4, 2, 1]
该方法用于对列表的元素进行排序
>>> num.sort()
>>> num
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
元组与列表类似,不同之处在于元组的元素不能修改,元组使用(),列表使用[],元组创建很简单,只需要在小括号中添加元素,并使用逗号隔开。
tupl1 = (1,2,3,4,5,6)
tupl2 = (‘alex‘,18,‘it‘)
tupl3 = ()
tupl4 = (100,)
>>> tupl1[-1]
6
>>> tupl1[2]
3
>>> tupl1[:5]
(1, 2, 3, 4, 5)
>>> tupl1[:4]
(1, 2, 3, 4)
>>> tupl1[2:4]
(3, 4)
>>> tupl1[2:-1]
(3, 4, 5)
>>>
>>> tupl3 = tupl1 + tupl2
>>> tupl3
(1, 2, 3, 4, 5, 6, ‘alex‘, 18, ‘it‘)
元组中的元素是不能修改的,可以使用del删除整个元组
>>> del tupl3
>>> tupl3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘tupl3‘ is not defined
>>> tuple(‘alex‘)
(‘a‘, ‘l‘, ‘e‘, ‘x‘)
>>> tuple([12,34,56])
(12, 34, 56)
>>>
>>> tupl1.count(2)
1
?
‘这就是字符串‘
"这还是字符串"
‘‘‘这还是字符串
还可以换行经?‘‘‘
str = "good good study and day day up"
>>> str.find(‘and‘)
16
>>>
>>> str.index(‘and‘)
16
>>> str.index(‘n‘)
17
>>> str.replace(‘and‘,‘&‘)
‘good good study & day day up‘
>>>
>>> ‘ good good study & day day up ‘.strip()
‘good good study & day day up‘
>>>
>>> str.capitalize()
‘Good good study and day day up‘
>>>
>>> ‘Hello‘.casefold()
‘hello‘
>>>
>>> name = ‘alex‘
>>> name.center(10)
‘ alex ‘
>>>
>>> name.center(10 ,‘*‘)
‘***alex***‘
>>>
>>> str
‘good good study and day day up‘
>>> str.count(‘g‘)
2
>>>
>>> str.endswith(‘p‘)
True
>>>
>>> ‘ALEX‘.lower()
‘alex‘
>>>
>>> name = ‘ alex ‘
>>> name.lstrip()
‘alex ‘
>>>
>>> name.rstrip()
‘ alex‘
>>>
字典是Python中唯一内建的映射类型,字典中的值是没有顺序。
dic = {‘alex‘:19,‘tony‘:20,‘eric‘:21}
>>> dic.clear()
>>> dic
{}
>>>
?
>>> dic.get(‘tony‘)
20
>>>
>>> dic.items()
dict_items([(‘alex‘, 19), (‘eric‘, 21), (‘tony‘, 20)])
>>>
>>> dic.keys()
dict_keys([‘alex‘, ‘eric‘, ‘tony‘])
>>>
>>> dic.values()
dict_values([19, 21, 20])
>>>
>>> dic.pop(‘alex‘)
19
>>> dic
{‘eric‘: 21, ‘tony‘: 20}
>>>
>>> new_dic ={‘alex‘:19}
{‘alex‘: 19}
>>>
>>> dic.update(new_dic)
>>> dic
{‘alex‘: 19, ‘eric‘: 21, ‘tony‘: 20}
>>>
标签:
原文地址:http://www.cnblogs.com/dongzhilong/p/5128837.html