字符串 数字 列表 元祖 字典 的不同分类and集合的概念

可变不可变

1、可变:列表 字典

2、不可变:字符串 数字 元祖

访问顺序:

1、顺序访问:字符串 列表 元祖

2、映射:字典

3、直接访问:数字

存放元素个数:

容器类型:列表 元祖 字典

原子:数字 字符串

id(变量名)可以查出储存的位置

s={1,2,3,3,9,8,8}
print(id(s))
41383080

 

集合(set):

1、不同元素组成

2、无序

3、集合中元素必须是不可变类型

例如:

s={1,2,3,3,9,8,8}
print(type(s))

输出

<class 'set'>
s = set('hellow')
print(s)

输出

{'h', 'w', 'o', 'l', 'e'}
s = set(['alex','alex','sh'])
print(s)

输出

{'sh', 'alex'}

有相同的元素自动合并。.add()的用法==〉添加

s = {1, 2, 3, 4, 5, 6}
print(s)
s.add('s')
s.add(3)
print(s)
s.add(3)
print(s)

输出

{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 's'}
{1, 2, 3, 4, 5, 6, 's'}

.clear()的用法==〉清空

s = {1, 2, 3, 4, 5, 6}
s.clear()
print(s)

输出

set()

.copy()的用法,==>浅拷贝

s = {1, 2, 3, 4, 5, 6}
s1=s.copy()
print(s1)

输出

{1, 2, 3, 4, 5, 6}

.pop()的用法,==〉随机删除一个元素

s = {1, 2, 3, 4, 5, 6}
s.pop()
s.pop()
print(s)

输出

{3, 4, 5, 6}
.remove()的用法,==〉指定删除一个元素,当指定元素不存在会报错
s = {1, 2, 3, 4, 5, 6}
s.remove(6)
print(s)

输出

{1, 2, 3, 4, 5}
.discard()的用法,==〉指定删除一个元素,当指定元素不存在不会报错(最好用这个)
s = {1, 2, 3, 4, 5, 6}
s.discard(6)
print(s)

输出

{1, 2, 3, 4, 5}

笨方法求交集

python_1 = ['lcg', 'szw', 'zjw']
linux_1 = ['lcg', 'szw']
python_1andlinux_1 = []
for p_name in python_1:
    if p_name in linux_1:
        python_1andlinux_1.append(p_name)
        print(python_1 and linux_1)

输出

['lcg', 'szw']

 

posted @ 2019-01-13 13:51  流水众生  阅读(288)  评论(0编辑  收藏  举报