小甲鱼教学笔记——字典、集合

1. 字典:属于映射类型,不是序列

  • {}
  • dict()
brand=['李宁','耐克','阿迪达斯','鱼C工作室']
slogan=['一切皆有可能','Just do it','Impossible is nothing','让编程改变世界']
print('鱼c工作室的口号是:',slogan[brand.index('鱼C工作室')])
鱼c工作室的口号是: 让编程改变世界
dict1={'李宁':'一切皆有可能','耐克':'Just do it','阿迪达斯':'Impossible is nothing','鱼C工作室':'让编程改变世界'}
print('鱼c工作室的口号是:',dict1['鱼C工作室'])
鱼c工作室的口号是: 让编程改变世界
dict2={1:'one',2:'two',3:'three'}
dict2[2]
'two'
dict3={}
dict3
{}
dict((('F',70),('i',105),('s',115),('h',104),('c',67)))
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'c': 67}
dict4=dict(小甲鱼='男的',苍井空='女的')#注意这里关键字不能加''
dict4
{'小甲鱼': '男的', '苍井空': '女的'}
dict4['苍井空']='哈哈哈'#修改关键字对应的值
dict4
{'小甲鱼': '男的', '苍井空': '哈哈哈'}
dict4['爱迪生']='发明家'#向字典中添加新元素
dict4
{'小甲鱼': '男的', '苍井空': '哈哈哈', '爱迪生': '发明家'}

1.1 fromkeys():重置

fromkeys(iterable, value=None, /)

  • 所有关键字对应一个值
dict1={}
dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
dict1.fromkeys((1,2,3),'哈哈哈')
{1: '哈哈哈', 2: '哈哈哈', 3: '哈哈哈'}
dict1=dict1.fromkeys(range(32),'赞')

1.2 keys():返回关键字

for i in dict1.keys():
    print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

1.3values:返回键值

dict1.values()
dict_values(['赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞', '赞'])

1.4 items:返回每一项

dict1.items()
dict_items([(0, '赞'), (1, '赞'), (2, '赞'), (3, '赞'), (4, '赞'), (5, '赞'), (6, '赞'), (7, '赞'), (8, '赞'), (9, '赞'), (10, '赞'), (11, '赞'), (12, '赞'), (13, '赞'), (14, '赞'), (15, '赞'), (16, '赞'), (17, '赞'), (18, '赞'), (19, '赞'), (20, '赞'), (21, '赞'), (22, '赞'), (23, '赞'), (24, '赞'), (25, '赞'), (26, '赞'), (27, '赞'), (28, '赞'), (29, '赞'), (30, '赞'), (31, '赞')])

1.5 get():获取字典内容

  • 字典中没对应项则返回为None
  • 可以用in/not in 来判断是否在字典中
dict1.get(31)
'赞'
dict1.get(32)
31 in dict1
True
32 in dict1
False

1.5 clear():清空

dict1.clear()
dict1
{}

1.6 copy():浅拷贝

a={1:'one',2:'two',3:'three'}
b=a.copy()
b
{1: 'one', 2: 'two', 3: 'three'}
id(a)
2831662158376
id(b)#地址不一样了,开辟了新的地址
2831662159576

1.7 pop():在字典中没有顺序,需要指定弹出元素

a.pop(2)
'two'
a
{1: 'one', 3: 'three'}

1.8 update():更新

b={"鱼":'好吃'}
a.update(b)#用b更新a
a
{1: 'one', 3: 'three', 5: 'five', '鱼': '好吃'}

2. 集合:

  • 唯一
  • 无序
  • 不支持索引
a={}
type(a)
dict
b={1,2,3,4,5}#集合
type(b)
set
c={1,2,3,3,3,4,5,5,5,5}#集合是唯一的
c
{1, 2, 3, 4, 5}
c[2]#集合不支持索引
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-59-827d9fd6e56a> in <module>
----> 1 c[2]#集合不支持索引


TypeError: 'set' object is not subscriptable

2.1 创建集合:

  • {}
  • set()函数
set1=set([1,2,2,3,3,4,5,5,5])
set1
{1, 2, 3, 4, 5}
a=[1,2,2,3,3,4,5,5,5]#如果不用集合。。。
t=[]
for i in a:
    if i not in t:
        t.append(i)
t
[1, 2, 3, 4, 5]

2.2 访问集合:

  • for循环
  • in 和 not in 判断
a={1,2,2,3,3,4,5,5,5}
1 in a
True
'1' in a
False
a.add(9)
a
{1, 2, 3, 4, 5, 9}
a.remove(1)
a
{2, 3, 4, 5, 9}

2.3 不可变集合:frozenset()

num=frozenset([1,2,3,4,5])
num.add(0)#报错,因为不可以修改
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-75-2cb1b59ac41b> in <module>
      1 num=frozenset([1,2,3,4,5])
----> 2 num.add(0)#报错,因为不可以修改


AttributeError: 'frozenset' object has no attribute 'add'

猜你喜欢

转载自blog.csdn.net/chairon/article/details/108027107
今日推荐