07.Pythonset collection, copy ⻉ depth knowledge as well as some supplementary points

⼀. Supplement basic data types

Int and str therefore especially on first before learning has been talked about more than 80% of the so rest Your Own see ⼀ see it.

We supplement to the basic operation ⼀ string

List:

Circulation delete the list each element ⼀

Analyze the reasons:

when running a process for the. ⼀ have a pointer to record the elements of the current cycle in which a ⼀, ⼀ started this pointer to 0

A. Then get to the 0th element. Then delete the first 0. This time. Turned out to be one of the first frame element will automatically become

The first 0. Then move the pointer back ⼀ times, point 1 elements. In this case the original one has become zero, there will not be deleted.

Using pop Delete try:

 

 

The analysis found. ⾏ not delete cycle. Use del whether or Use remove. Can not be realized. Then pop it?

Only in this way it is possible:

Or. Using a list for another time to record what you want to delete. Then delete the cycle

Note: Due to remove elements will lead to changes in the index of elements, so prone to problems try not to go directly to the recycle deleted.

In addition to the elements can be added to the element to be removed for another time set and then bulk delete.

dict in fromkey (), you can help us to create a dict ⼀ by list

Before ⾯ list each item will ⼀ as a key, after ⾯ list content as a value. ⽣ to dict

Well Note:

Just change the code jay that list. However, due to jay and JJ Using the same ⼀ lists. So before ⾯ that changed after

⾯ the change to follow

dict the elements in the iterative process is not allowed to enter ⾏ deleted

How to do it? The elements to be deleted before being saved in ⼀ a list, and then the cycle list, then deleted

Type conversion:

Tuple => list list (tuple)

List => tuple tuple (list)

list=>str str.join(list)

str=>list str.split()

False data is converted into:

0,'',None,[],(),{},set() ==> False

⼆. Set collection

collection is a set python ⼀ basic data types. shoots as usual is not very frequently-used. The set of elements is not repeated. ⽆ sequence. ⾥

⾯的元素必须是可hash的(int, str, tuple,bool), 我们可以这样来记. set就是dict类型的数据但

是不保存value, 只保存key. set也⽤{}表⽰

注意: set集合中的元素必须是可hash的, 但是set本⾝是不可hash得. set是可变的.

set中的元素是不重复的, 且⽆序的.

使⽤这个特性.我们可以使⽤set来去掉重复

set集合增删改查

1. 增加

2. 删除

3. 修改

4. 查询

5. 常⽤操作

 

 

 

set集合本⾝是可以发⽣改变的. 是不可hash的. 我们可以使⽤frozenset来保存数据.

frozenset是不可变的. 也就是⼀个可哈希的数据类型

这个不是很常⽤. 了解⼀下就可以了

三. 深浅拷⻉

 

对于list, set, dict来说, 直接赋值. 其实是把内存地址交给变量. 并不是复制⼀份内容. 所以.

lst1的内存指向和lst2是⼀样的. lst1改变了, lst2也发⽣了改变

浅拷⻉

 

 

 

 

都不⼀样了. 深度拷⻉. 把元素内部的元素完全进⾏拷⻉复制. 不会产⽣⼀个改变另⼀个跟着

改变的问题

补充⼀个知识点:

最后我们来看⼀个⾯试题:

 

Guess you like

Origin www.cnblogs.com/xuweng/p/12169304.html