tuple、dict、set的增删改查

第四章:tuple、dict、set的增删改查

小点:Len统计list中有多少个元素。
规则:a为空的话,在表达式里面是错误的,如果有元素的话就是True。

>>> if a:
...    print("not a null list ")
... else:
...    print("a null list")
... 
not a null list 
>>> a
[0]

在这里插入图片描述

1、“bool”查看True/False

⚠️只要是空的就是False,只要有数值就为True(0要做特殊的处理)

>>> bool(1)
True
>>> bool(None)
False

在这里插入图片描述
练习题:求1+2+3.、、、、+10的和?

sum=0
for i in range(11):
    sum=sum+i
    print(i,sum)

在这里插入图片描述

2、tuple-元组

元组与列表的区别,不能改遍元组中的对象。

>>> a=(1,"3",2,[20,34,323],{1:10})
>>> a
(1, '3', 2, [20, 34, 323], {1: 10})
>>> del a[3][0]
>>> a
(1, '3', 2, [34, 323], {1: 10})
>>> a[3][1]=23322
>>> a
(1, '3', 2, [34, 23322], {1: 10})
>>> a[4][1000]="XXX"
>>> a
(1, '3', 2, [34, 23322], {1: 10, 1000: 'XXX'})


在这里插入图片描述
⚠️单个元素要做元组的时候,需要在元素后面加逗号

>>> a=(1)
>>> type(a)
<class 'int'>
>>> a=(1,)
>>> type(a)
<class 'tuple'>
>>> a=()
>>> type(a)
<class 'tuple'>
>>> a=("a")
>>> type(a)
<class 'str'>
>>> a=("a",)
>>> type(a)
<class 'tuple'>
>>> a = 1,2
>>> print(a)
(1, 2)
>>> type(a)
<class 'tuple'>

在这里插入图片描述

3、dict-字典

字典无顺序,成对的方式存在,前面的是key后面的是value
(1)dict—增加

>>> d = {}
>>> type(d)
<class 'dict'>
>>> d={1:2,"a":100}
>>> d
{1: 2, 'a': 100}
>>> d["XX"]=434.23
>>> d
{1: 2, 'a': 100, 'XX': 434.23}

在这里插入图片描述
(2) dict-改

>>> d
{1: 2, 'a': 100, 'XX': 434.23}
>>> d[1]="ab"
>>> d
{1: 'ab', 'a': 100, 'XX': 434.23}
>>> 

在这里插入图片描述
(3)dict-删除

>>> d
{1: 'ab', 'a': 100, 'XX': 434.23}
>>> del d["XX"]
>>> d
{1: 'ab', 'a': 100}

在这里插入图片描述

>>> d[1]
'sds'
>>> d['ds']
100
>>> d.pop('ds')
100
>>> d
{1: 'sds'}
>>> del d[1]
>>> d
{}

在这里插入图片描述

>>> d={1:1,2:2,3:3,4:4}
>>> d
{1: 1, 2: 2, 3: 3, 4: 4}
>>> d[1]
1
>>> d.keys()
dict_keys([1, 2, 3, 4])
>>> list(d.keys())
[1, 2, 3, 4]
>>> type(d.keys())
<class 'dict_keys'>

在这里插入图片描述

4、字典key和value的遍历

⚠️字典key的特点:不能重复,不能使用可变的对象

>>> d
{1: 1, 2: 2, 3: 3, 4: 4}
>>> type(d.keys())
<class 'dict_keys'>
>>> for key in d.keys():
...     print(key)
... 
1
2
3
4
>>> for value in d.values():
...     print(value)
... 
1
2
3
4

在这里插入图片描述

>>> for key,value in d.items():
...     print(key,":",value)
... 
1 : 1
2 : 2
3 : 3
4 : 4

在这里插入图片描述

>>> print(1)
1
>>> print("a")
a
>>> print("a",end="")
a>>> print("b",end="")
b>>> print("b","c",end="")
b c>>> print("b","c","d",end="")
b c d>>> print("b","c","d")
b c d
>>>

在这里插入图片描述

5、fromkeys快速生成字典对象的key和value键值对
>>> d={}
>>> d.fromkeys([1,2,3])
{1: None, 2: None, 3: None}
>>> d.fromkeys([1,2,3],"a")
{1: 'a', 2: 'a', 3: 'a'}
>>> d.fromkeys([1,2,3],"a")
{1: 'a', 2: 'a', 3: 'a'}
>>> d=dict.fromkeys([1,2,3],"a")
>>> d
{1: 'a', 2: 'a', 3: 'a'}

在这里插入图片描述
在这里插入图片描述

6、update
>>> d=dict.fromkeys([1,2,3])
>>> d
{1: None, 2: None, 3: None}
>>> d.update({4:4,5:5,6:6})
>>> d
{1: None, 2: None, 3: None, 4: 4, 5: 5, 6: 6}
>>> d.update([1,2,3,4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
>>> d.update(((7,7),(8,8)))
>>> d
{1: None, 2: None, 3: None, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}

在这里插入图片描述
在这里插入图片描述

>>> d
{1: None, 2: None, 3: None, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
>>> d=dict(name="sunqiainli",sex="wo")
>>> d
{'name': 'sunqiainli', 'sex': 'wo'}
>>> d = {x:x**2 for x in (2,4,6)}
>>> d
{2: 4, 4: 16, 6: 36}
>>> 

在这里插入图片描述
面试题:统计一句话中,请统计一下一共出现了多少个单词?每个英文单词出现的次数,
例如:I am a boy ,I am a good man!
思路:1、首先创建一个字符串,然后用Split分割,还有创建一个空的健值对
2、循环这句话的每一个字母,首次出现的加入为key,非首次加入的次数加1.
3、 最后输出循环后的key和value

a='I am a boy.I am a good man!'
def letter(a):
    if not isinstance(a,str):
        print("the parameter is not str")
        return 0
    result=0
    for i in a:
       if i in  "abcdefghigklmnopqrstuvwxyz":
           result+=1
    return result
print(letter(a))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值