0507Python basics-set-depth copy

1. Supplement of basic data types.

1, tuple
tu1 = ('laonanhai')
tu2 = ('laonanhai',)
print(tu1, type(tu1))
print(tu2, type(tu2))

tu1 = (1)
tu2 = (1,)
print( tu1, type(tu1))
print(tu2, type(tu2))

If there is only one element in the tuple and there is no comma, the data type of the data is the same as the element in it.
tu1 = ([1, 2, 3])
tu2 = ([1, 2, 3],)
print(tu1, type(tu1))
print(tu2, type(tu2))

list
l1 = [111, 222, 333 , 444, 555, ]
All elements corresponding to odd indices are deleted.
Method 1:
del l1[1::2]
print(l1)
Method 2 Error display:
for index in range(len(l1)):
    print('delete before index:%s' % index)
    print('before delete l1:%s' % l1)
    if index % 2 == 1:
        del l1[index]
    print('index after deletion: %s' % index)
    print('l1 after deletion: %s' % l1)
print(l1)
When looping through a list, it is best not to change the size of the list, it will affect your Final Results.
Delete backwards.
for index in range(len(l1)-1, -1, -1):
    if index % 2 == 1:
        del l1[index]
print(l1)
dict
dic = dict.fromkeys('abc',666)
print (dic)
dic = dict.fromkeys([11,22,33],666)
print(dic)
dic = dict.fromkeys([1,2,3],[])
dic[1].append(666)
print (dic)

In the loop dict, it is best not to change the size of the dict, which will affect the result or report an error.
dic = {'k1': 'v1', 'k2': 'v2','k3': 'v3','name': 'alex'}
for i in dic:
    if 'k' in i:



    if 'k' in key:
        l1.append(key)
# print(l1)
for key in l1:
    del dic[key]
print(dic)

数据类型的转换:

str ---> list split
list --> str join

tuple ---> list
tu1 = (1, 2, 3)
l1 = list(tu1)
print(l1)
tu2 = tuple(l1)
print(tu2)

dic ----> list  list(dic)(列表中的元素只有key)

dic = {'k1': 'v1', 'k2': 'v2','k3': 'v3',}
l1 = list(dic)
print(l1)
print(list(dic.keys()))
print(list(dic.values()))
print(list(dic.items()))

0, '', [], () {}  ---> bool 都是Fasle
print(bool([0, 0, 0]))

 

 

2. The basic data type set.

        Collection is a variable data type, it exists in the form of {}, empty collection set(),

    But it requires the elements in it to be immutable, and the collection to be unordered and non-repeating.

        1. Go heavy.

        2. Relationship test.


set1 = {1, 2, 3, 'abc', (1,2,3), True, }
print(set1)
set2 = {11, 11, 11, 22}
print(set2)
list deduplication
l1 = [ 11, 11, 22, 22, 33, 33, 33, 44]
l2 = list(set(l1))
l2.sort()
print(l2)

set1 = {'alex', 'WuSir', 'RiTiAn', ' egon', 'barry'}
add
set1.add('Taibai')
print(set1)

set1.update('abc')
set1.update([111, 2222, 333])
print(set1)
delete
remove remove
set1 by element .remove('RiTiAn')
print(set1)
delete randomly, there is a return value pop
print(set1.pop())
print(set1)

clear
set1.clear()
print(set1)

delete the whole del
del set1
print(set1)

# Check
for i in set1:
    print(i)

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
intersection & intersection
set3 = set1 & set2
print(set3)
print (set1.intersection(set2))
union | union
print(set1 | set2)
print(set1.union(set2))
difference - difference
print(set1 - set2) # set1 unique
print(set2.difference(set1)) # set2 unique

anti-intersection ^ symmetric_difference
print(set1 ^ set2)
print(set1.symmetric_difference(set2))

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5, 6}

print(set1 < set2) # True set1 is a subset of set2
print(set1.issubset(set2)) # True set1 is a subset of set2

print(set2 > set1) # True set2 is a superset of set1
print(set2.issuperset(set1)) # True set2 is a superset of

set1 set1 = frozenset({1, 2, 3, 'alex'})
print(set1,type(set1))

 

04, depth copy.

 

l1 = [1, 2, 3]
l2 = l1
l3 = l2
l3.append(666)
print(l1, l2, l3)
For assignment operations, they all point to the same memory address, and they all change.
light copy

light copy
l1 = [11, 22, 33]
l2 = l1.copy()
l1.append(666)
print(l1, id(l1))
print(l2, id(l2))
l1 = [11, 22 , ['barry', [55, 66]], [11, 22]]
l2 = l1.copy()
l1[2].append('alex')
print(l1,id(l1))
print(l2, id(l2))
print(l1, id(l1[-1]))
print(l2, id(l2[-1]))
For shallow copy, for shallow copy, the first layer creates new From the second layer
, it points to the same memory address, so for the second layer and deeper layers, consistency is maintained.

deep copy
import copy
l1 = [11, 22, 33]
l2 = copy.deepcopy(l1)
l1.append(666)
print(l1, id(l1))
print(l2, id(l2))

l1 = [11, 22, ['barry']]
l2 = copy.deepcopy(l1)
l1[2].append('alex')
print(l1, id(l1[-1]))
print(l2, id(l2[-1]))
deep copy completely independent.
s1 = 'alex'
s2 = 'alex'
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l1.append(666)
print(l1, l2)

For slices, this is a shallow copy.
l1 = [1, 2, 3, 4, 5, 6, [11,22]]
l2 = l1[:]
l1.append(666)
print(l1, l2)
l1[-1].append(666)
print (l1, l2)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648090&siteId=291194637