首页 > 代码库 > Python List+Tuple+Dict+Set小结

Python List+Tuple+Dict+Set小结

显示List:L[0]

遍历List:print (L)和for循环

更新List:append()和insert()和直接赋值

删除List:pop()和pop(n)

List特点:

可以添加删除

?
1
2
3
4
5
6
Li = [‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘]
 
print (Li)
 
for name in Li:
    print(name)

  

 

创建Tuple:Tu = (‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘)

显示Tuple:Tu[0]

遍历Tuple:print (Tu)和for循环

更新Tuple:不可以更新

删除Tuple:不可以删除

Tuple特点:不可以改变,如果创建之后一个元素的Tuple时,要加","

?
1
2
3
4
5
6
Tu = (‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘)
 
print (Tu)
 
for name in Tu:
    print(name)

  

创建Dict:Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}

显示Dict:Di[‘Adam‘]和Di.get(‘Adam‘)

遍历Dict:print(Di)和for循环

 

?
1
2
3
4
Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}
 
for key in Di:
    print (key)

  

?
1
2
3
4
Di = {‘Adam‘:100, ‘Lisa‘:90, ‘Bart‘:93, ‘Gechong‘:100, ‘Kongming‘:100}
 
for key in Di:
    print (Di[key])

  

更新Dict:对应位置赋值即可

删除Dict:

Dict特点:

1:查找速度快

2:浪费空间

3:key不可以重复,且不可变

4:数据无序排放

 

创建set:s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])

显示set:

?
1
2
3
4
5
6
s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])
 
if ‘Test‘ in s:
    print (‘Test‘)
else:
    print (‘No‘)

  

遍历set:print (s)

?
1
2
3
4
s = set([‘Adam‘, ‘Lisa‘, ‘Bart‘, ‘Gechong‘, ‘Kongming‘])
 
for name in s:
    print (name)

  

更新set:s.add()

删除set:s.remove()

Python List+Tuple+Dict+Set小结