list、tuple、dict和set

列表list

list(列表)是Python中最基本的数据结构,列表中的元素可以是 数字,字符串,列表,布尔值以及其他数据结构

一、通用操作

1,,可通过索引取值、修改;进行for循环;切片

list1 = ['you','he','she','it','me']
print(list1[2])                             #索引取值
print(list1[2:-1])                           #切片取值
for item in list1:                          #for循环遍历
    print(item)
list1[1] = '他'                              #通过索引修改
print(list1)
list1[1:3] = [['he','他'],'她']                    #切片修改
print(list1)
print(list1[1][1])                           #索引取二级元素    
she
['she', 'it']
you
he
she
it
me
['you', '他', 'she', 'it', 'me']
['you', ['he', '他'], '她', 'it', 'me']
他

2,通过del语句删除元素

list1 = ['you','he','she','it','me']
del list1[1]
print(list1)
['you', 'she', 'it', 'me']

3,通过in判断元素存在

list1 = ['you','he','she','it','me']
ll = 'he' in list1
print(ll)
True

4,+ 和 * 操作

list1 = ['you','he','she']
list2 = ['it','me']
list3 = list1 + list2
list4 = list3 * 2
print(list3)
print(list4)
['you', 'he', 'she', 'it', 'me']
['you', 'he', 'she', 'it', 'me', 'you', 'he', 'she', 'it', 'me']

5,len()统计元素

list1 = ['you','he','she','it','me']
print(len(list1))
5

6,字符串与列表相互转换

list1 = ['you','he','she','it','me',11,22,33]
list2 = ['you','he','she','it','me']
str1 = str(list1)                       #直接套用只是将整个列表变为字符串
str2 = ''
str3 = ''                               
for item in list1:                     #列表中有数字int元素需用for循环
    str2 += str(item)
str3 = ''.join(list2)                   #列表无数字可以用join
print(str1)
print(str2)
print(str3)
['you', 'he', 'she', 'it', 'me', 11, 22, 33]
youhesheitme112233
youhesheitme
str1 = 'abcdefgh'                         #使用list方法将字符串变成列表
print(list(str1))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

二、类方法

1、append()追加到末尾

list1 = ['you','he','she','it','me',11,22,33]
list1.append(['你','他'])
print(list1)
['you', 'he', 'she', 'it', 'me', 11, 22, 33, ['你', '他']]

2、extend()扩展

在列表末尾一次性追加另一个序列中的多个值(如元组、列表)
list1 = ['you','he','she','it','me',11,22,33]
list1.extend(('你','他'))
print(list1)
list1.extend(['你','他'])
print(list1)
['you', 'he', 'she', 'it', 'me', 11, 22, 33, '你', '他']
['you', 'he', 'she', 'it', 'me', 11, 22, 33, '你', '他', '你', '他']

3、clear()清空列表

list1 = ['you','he','she','it','me',11,22,33]
list1.clear()
print(list1)
[]

4、copy()浅拷贝

list1 = ['you','he','she','it','me',11,22,33]
list2 = list1.copy()
print(list1)
print(list2)
['you', 'he', 'she', 'it', 'me', 11, 22, 33]
['you', 'he', 'she', 'it', 'me', 11, 22, 33]

5、count()计算元素出现次数

list1 = ['you','he','she','it','me',11,22,33]
lis = list1.count('it')
print(lis)
1

6、index()从左到右查找给定值所在索引

list1 = ['you','he',11,'she','it','me',11,22,33]
print(list1.index(11))
2

7、insert()在指定位置插入元素

list1 = ['you','he',11,'she','it','me',11,22,33]
list1.insert(1,'kid')
print(list1)
['you', 'kid', 'he', 11, 'she', 'it', 'me', 11, 22, 33]

8、pop()删除指定位置元素

list1 = ['you','he',11,'she','it','me',11,22,33]
list_pop = list1.pop(1)                    #将删除元素赋值给list_pop(即pop()方法有返回值即所删元素)
print(list_pop)
print(list1)
he
['you', 11, 'she', 'it', 'me', 11, 22, 33]

9、remove()从左到右找到指定元素并删除

list1 = ['you','he',11,'she','it','me',11,22,33]
list1.remove(11)
print(list1)
['you', 'he', 'she', 'it', 'me', 11, 22, 33]

10、reverse()翻转列表

list1 = ['you','he',11,'she','it','me',11,22,33]
list1.reverse()
print(list1)
[33, 22, 11, 'me', 'it', 'she', 11, 'he', 'you']

11、sort()排序

参数可以加上reverse=true,翻转顺序

list1 = [112,556,897,424,354,25,3]
list1.sort()
print(list1)
[3, 25, 112, 354, 424, 556, 897]

元组tuple

元组一级元素不可修改、增加、删除。但是可修改的次级元素可以进行操作

1、索引与切片取值

tu = ([12,34],[34,67],435,67,)
print(tu[2])
print(tu[1:])
435
([34, 67], 435, 67)

2、迭代取值

tu = ([12,34],[34,67],435,67,)
for item in tu:
    print(item)
[12, 34]
[34, 67]
435
67

3、字符串、列表转换元组

s = 'kslegnsdg'
li = ['ksd','34','56',78]
tu1 = tuple(s)
tu2 = tuple(li)
print(tu1)
print(tu2)
('k', 's', 'l', 'e', 'g', 'n', 's', 'd', 'g')
('ksd', '34', '56', 78)

元组转列表

tu = ('ksd', '34', '56', 78)
li = list(tu)
print(li)
['ksd', '34', '56', 78]
#元组转字符串
tu = ('ksd', '34', '56', '78')
s = str(tu)
s2 = ''.join(tu)
print(s)
print(s2)            
tu1 = ('ksd', '34', '56', 78)    #如果元组包含数字需用迭代方法转换
s3 = ''
for item in tu1:
    s3 += str(item)
print(s3)
('ksd', '34', '56', '78')
ksd345678
ksd345678

字典dict

字典内键值对应,布尔值(1,0)、列表、字典不能作为字典的key(键),不允许同一个键出现两次,字典的value可以是任何值,字典无序,可以通过索引[key]取[value]

通过del 删除键值对

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
del dic['h4']
print(dic)
{'h1': 'a', 'h2': 'b', 'h3': 'c'}

通过迭代循环取到keys

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
for item in dic:
    print(item)
h1
h2
h3
h4

类方法

1、fromkeys()根据序列创建字典并指定统一值

dic = dict.fromkeys(['k1','k2','k3','k4'],['1','2','3','4'])
print(dic)
{'k1': ['1', '2', '3', '4'], 'k2': ['1', '2', '3', '4'], 'k3': ['1', '2', '3', '4'], 'k4': ['1', '2', '3', '4']}

2、get()根据Key获取值,key不存在时,可以指定默认值(None)

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.get('h1')
print(v)
a
dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.get('k1')
print(v)
None
dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.get('k1',404)
print(v)
404

3、pop()删除并获取值;popitem()随机删除并获取键值

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.pop('h1')
print(dic,v)
{'h2': 'b', 'h3': 'c', 'h4': 'd'} a
dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.popitem()
print(dic,v)
{'h1': 'a', 'h2': 'b', 'h3': 'c'} ('h4', 'd')

4、setdefault()设置值
* 已存在,不设置,获取当前key对应的值
* 不存在,设置,获取当前key对应的值

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.setdefault('h1',123)
print(v)
print(dic)
a
{'h1': 'a', 'h2': 'b', 'h3': 'c', 'h4': 'd'}
dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
v = dic.setdefault('k1',123)
print(v)
print(dic)
123
{'h1': 'a', 'h2': 'b', 'h3': 'c', 'h4': 'd', 'k1': 123}

5、update()更新

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
dic.update({'k1':11112,'h1':22225})
print(dic)
{'h1': 22225, 'h2': 'b', 'h3': 'c', 'h4': 'd', 'k1': 11112}
dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
dic.update(k1=11112,h1=22225)
print(dic)
{'h1': 22225, 'h2': 'b', 'h3': 'c', 'h4': 'd', 'k1': 11112}

6、keys()、values()、items() 通过列表返回可以遍历的键、值、键值对

dic = {'h1':'a','h2':'b','h3':'c','h4':'d'}
print(dic.keys())
print(dic.values())
print(dic.items())
dict_keys(['h1', 'h2', 'h3', 'h4'])
dict_values(['a', 'b', 'c', 'd'])
dict_items([('h1', 'a'), ('h2', 'b'), ('h3', 'c'), ('h4', 'd')])

集合set

增加元素

1、add()添加一个元素到集合

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.add('c')
print(sss)
{1, 2, 3, 5, 'b', 'a', 'c'}

2、update()增加多个集合

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.update((3 , 'c' , 'd'))
print(sss)
{1, 2, 3, 5, 'd', 'b', 'a', 'c'}

删除元素

1、clear()清空集合所有元素

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.clear()
print(sss)
set()

2、pop()随机删除一个元素

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.pop()
print(sss)
{2, 3, 5, 'b', 'a'}

3、remove()删除一个指定元素,如果集合中不存在会报错

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.remove(2)
print(sss)
{1, 3, 5, 'b', 'a'}

4、discard()删除指定元素,集合中不存在不会报错

sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss.discard(21)
print(sss)
{1, 2, 3, 5, 'b', 'a'}

集合创建

sss = set()             #创建一个空集
sss1 = set([1,2,3])      #参数可以使用列表和元组,也可以通过此方法转换为集合
sss2 = {1,2,3,4,5}        #直接创建
sss3 = frozenset('hello')  #创建不可修改集合
  • copy()浅拷贝
sss = {1 , 2, 3 , 5, 'a' , 'b'}
sss1 = sss.copy()
print(sss1)
{1, 2, 3, 5, 'b', 'a'}

集合运算

1、通过intersection() 或 & 来求交集

s1 = {'abc','123','fff','gg','bf'}
s2 = {'abc','123','mm','gf'}
print(s1.intersection(s2))
print(s1 & s2)
{'123', 'abc'}
{'123', 'abc'}

2、通过union()或|来求并集

s1 = {'abc','123','fff','gg','bf'}
s2 = {'abc','123','mm','gf'}
print(s1.union(s2))
print(s1|s2)
{'bf', 'gg', 'mm', '123', 'abc', 'fff', 'gf'}
{'bf', 'gg', 'mm', '123', 'abc', 'fff', 'gf'}

3、通过difference()或 - 来求差集

s1 = {'abc','123','fff','gg','bf'}
s2 = {'abc','123','mm','gf'}
print(s1.difference(s2))
print(s2 - s1)
{'bf', 'gg', 'fff'}
{'gf', 'mm'}

4、通过symmetric_difference()或^来求交叉补集

s1 = {'abc','123','fff','gg','bf'}
s2 = {'abc','123','mm','gf'}
print(s1.symmetric_difference(s2))
print(s2 ^ s1)
{'bf', 'gg', 'gf', 'mm', 'fff'}
{'bf', 'gg', 'mm', 'fff', 'gf'}

5、difference_update()求出差集并更新到原集合

s1 = {'abc','123','fff','gg','bf'}
s2 = {'abc','123','mm','gf'}
s1.difference_update(s2)
print(s1)
{'bf', 'gg', 'fff'}

6、判断集合关系

s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))    #s1 是s2 的子集
print(s2.issubset(s1))    #False

print(s2.issuperset(s1))  #s1 是s2 的父集
True
False
True

猜你喜欢

转载自blog.csdn.net/marvin_wind/article/details/79914624