1,判断字符串是不是空格isspace函数

s1 = ' '
s2 = ' ssss'
print(s1.isspace())
print(s2.isspace()) 运行结果:
True
False

2,list和dict不可以边循环边删除,删除其中一个,index就变了

li = ["apple","pear","peach","watermelon",'grape']
# 要求删除里面所有的项
for i in range(len(li)):
del li[i] # 错误原因,不可以边循环,边删除,因为删除每一个之后,index就变了,后面的循环就无效了,dict也是这个原因 运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.1.py", line 18, in <module>
del li[i]
IndexError: list assignment index out of range
dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"}

# 删掉name的key
for i in dict1:
if "name" in i:
del dict1[i] 运行结果:
Traceback (most recent call last):
  File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.1.py", line 25, in <module>
    for i in dict1:
RuntimeError: dictionary changed size during iteration

3,如何解决上面的问题呢?利用逆向思维,找出要删除的项之外的项,放到另外一个dict或者list里面,然后在赋值给原来的list即可

dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"}
for i in dict1:
if "name" not in i:
dict2.setdefault(i,dict1[i]) dict1 = dict2
print(dict1.items()) 运行结果:
dict_items([('hobby', 'piano')])

4,如果不想这样的话,可以单独把要删除的项找出来,组成一个单独的list,然后再删除,只是不要一边循环原来的字典一边从原来的的字典里面删除就行,因为这样会打乱已有的循环动作

dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"}
li2 = []
for i in dict1:
if "name" in i:
li2.append(i) for i in li2: # 发现没循环的不是原来的dict,是新生成的列表
del dict1[i] print(dict1.items()) 运行结果:
dict_items([('hobby', 'piano')])

5,空字典 { }  空集合set(),由于大括号给字典用了了,空字典[] ,空元祖() ,所有这些空值,bool值是false,元祖只有一个值的时候注意加上一个逗号,不然原来是什么类型,还是什么类型

print(type(1))
print(type(1,)) print(type("sss"))
print(type("sss",)) 运行结果:
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>

6,集合是可变的数据类型,它里面的数据类型,必须是不可变的数据类型。不可变,意味着可哈希

set1 = {1,2,3,(4,5),"sss",True}
print(set1) 运行结果:
{1, 2, 3, (4, 5), 'sss'} set2 = {"str",[1,2,3],{"name":"alex"}}
print(set2)
运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 12, in <module>
set2 = {"str",[1,2,3],{"name":"alex"}}
TypeError: unhashable type: 'list'

7,集合添加元素add函数

set1 = {1,2,3,(4,5),"sss",True}
print(set1)
set1.add("lisa")
print(set1) # 集合是无序的,找一找,看看新加的元素添加到哪儿啦? 运行结果:
{1, 2, 3, (4, 5), 'sss'}
{'lisa', 1, 2, 3, (4, 5), 'sss'}

8,set也可以迭代增加,把增加的元素,按照最小单位拆分添加update

set1 = {1,2,3,(4,5),"sss",True}
print(set1)
set1.add("lisa")
print(set1) # 集合是无序的,找一找,看看新加的元素添加到哪儿啦?
set1.update("bobocddd") # 这儿无序,去重,迭代添加都体现出来了
print(set1) 运行结果:
{1, 2, 3, (4, 5), 'sss'}
{1, 2, 3, (4, 5), 'lisa', 'sss'}
{1, 2, 3, 'b', (4, 5), 'd', 'lisa', 'o', 'c', 'sss'}

9,删除,pop (remove return arbitrary refer to the introduction of this function)

set1 = {1,2,3,(4,5),"sss",True}
print(set1.pop())
print(set1) 运行结果:
1
{2, 3, (4, 5), 'sss'}

10,删除的另外一种方法,remove ,无返回值,但是如果指定的元素不存在的话,程序会崩溃

set1 = {1,2,3,(4,5),"sss",True}
print(set1.remove("sss"))
print(set1) # 无返回值
运行结果:
None
{1, 2, 3, (4, 5)}
set1 = {1,2,3,(4,5),"sss",True}
print(set1.remove("XXX",))
print(set1) 运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 28, in <module>
print(set1.remove("XXX",))
KeyError: 'XXX'

11,清空集合 clear函数,

12,删除集合 del 函数 ,注意清空完是空集合,del完集合就彻底没了

13,集合也可以遍历,按照for....in....

14,求交集,就是数学概念里面的交集 符号& 或者函数intersection

set1 = {1,2,3,(4,5),"sss",True}
set2 = {3,(4,5),"kkk"}
print(set1&set2)
print(set1.intersection(set2)) 运行结果:
{(4, 5), 3}
{(4, 5), 3}

15,求并集,符号|和函数union

set1 = {1,2,3,(4,5),"sss",True}
set2 = {3,(4,5),"kkk"} print(set1|set2)
print(set1.union(set2)) 运行结果:
{'sss', 1, 2, 3, 'kkk', (4, 5)}
{'sss', 1, 2, 3, 'kkk', (4, 5)}

16,反交集 符号^ 或者函数symmetric_difference

set1 ={1,2,3,4,5,6,7}
set2 ={4,5,6,7,8,9,0}
print(set1^set2)
print(set1.symmetric_difference(set2)) 运行结果:
{0, 1, 2, 3, 8, 9}
{0, 1, 2, 3, 8, 9}

17,set1 独有的,符号-和different函数

set1 ={1,2,3,4,5,6,7}
set2 ={4,5,6,7,8,9,0} print(set1-set2) # set1独有的
print(set2-set1) # set2独有的 print(set1.difference(set2))
print(set2.difference(set1)) 运行结果:
{1, 2, 3}
{0, 8, 9}
{1, 2, 3}
{0, 8, 9}

18,子集与超集合 符号> 和符号<

set1 ={1,2,3,4,5,6,7}
set2 ={1,2,3,4} print(set1>set2) #超集
print(set1.issuperset(set2)) #超集
print(set2<set1) #子集
print(set2.issubset(set1)) #子集 运行结果:
True
True
True
True

19,列表去重,先转化为set再把set转化为列表

li = [1,2,3,4,5,6,6,5,4,3,7,6,5,4]
set1 =set(li)
print(set1)
li=list(set1)
print(li) 运行结果:
{1, 2, 3, 4, 5, 6, 7}
[1, 2, 3, 4, 5, 6, 7]

20,深浅copy 浅copy不会改变孙子里面地址,只是儿子地址变了,深copy完完全全一个新的,函数copy和deepcopy

21,enumerate函数

li = ['lisa', 'mary', 'lucy', 'hh']
for i in enumerate(li):
print(i) 运行结果:
(0, 'lisa')
(1, 'mary')
(2, 'lucy')
(3, 'hh') # 可以指定起始位置,自我感觉用处不是很大:
li = ['lisa', 'mary', 'lucy', 'hh']
for i in enumerate(li,3):
print(i) 运行结果:
(3, 'lisa')
(4, 'mary')
(5, 'lucy')
(6, 'hh')

22,集合变成不可变的,frozenset

set1 ={1,2,3,4,5,6,7}
set1=frozenset(set1) #生成一个新的set,注意要赋值回去
print(set1,type(set1))
set1.add(9) 运行结果:
frozenset({1, 2, 3, 4, 5, 6, 7}) <class 'frozenset'>
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 67, in <module>
set1.add(9)
AttributeError: 'frozenset' object has no attribute 'add'
05-11 13:02