Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the replication method for python collection set and creating objects

Shulou Source: shulou.com Published: 2022-06-01 03:54:37 09月29日 Update

This article Xiaobian introduces in detail for you "python collection set and create object replication method is what", the content is detailed, the steps are clear, the details are handled properly, I hope that this "python collection set and create object replication method is what" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.

Set set

Enter:

Bri = set (['brazil',' russia', 'india'])

Print ('india' in bri)

Print ('usa' in bri)

Bric = bri.copy ()

Bric.add ('china')

Print (bric.issuperset (bri))

Bri.remove ('russia')

Print (bri & bric)

# OR bri.intersection (bric)

Output:

True

False

True

{'brazil',' india'}

Explanation:

A collection set is an unordered collection of simple objects. Compared with sequential data structures, collections pay more attention to whether the objects in the collection exist and how many times they occur.

In this example, the set collection is first defined as bri. Exe.

The in statement is then used to determine whether the two objects' india','usa', are in the collection, and the output returns the Bool value TRUE or False depending on whether the object is in the collection.

The collection has the copy () function, which can copy a new collection.

The collection can call .remove () to delete one of the objects.

You can enter the set operation, and in this case, take the intersection set operation.

The output is: {'brazil',' india'}

Create a copy operation for an object

Enter:

#! / usr/bin/python

# Filename: reference.py

Print ('Simple Assignment')

Shoplist = ['apple',' mango', 'carrot',' banana'] mylist = shoplist

# mylist is just another name pointing to the sameobject!

Del shoplist [0]

# I purchased the first item, so I remove it from thelist

Print ('shoplist is', shoplist)

Print ('mylist is', mylist)

# notice that both shoplist and mylist both print the same list without

# the 'apple' confirming that they point to the same object

Print ('Copy by making a full slice')

Mylist = shoplist [:] # make a copy by doing a full slice

Del mylist [0]

# remove first item

Print ('shoplist is', shoplist)

Print ('mylist is', mylist)

# notice that now the two lists are different

Output:

$python reference.py

Simple Assignment

Shoplist is ['mango',' carrot', 'banana']

Mylist is ['mango',' carrot', 'banana']

Copy by making a full slice

Shoplist is ['mango',' carrot', 'banana']

Mylist is ['carrot',' banana']

Explanation:

When you create an object and assign it to a variable, the variable only refers to the object, not the object itself! It can be understood as assigning an address link.

Two sets of examples are given in this example.

First create a list shoplist, and we copy a list mylist. The copy method uses the "=" assignment.

We deleted an element in shoplist and found that the element in mylist had also been deleted.

The second time, we also copied a list mylist. The copy method is assigned using "mylist = shoplist [:]".

We delete an element in shoplist and find that the element in mylist still exists.

In the first, we only copy the address mapping of the object, and the second time, we fully assign the contents of the object.

After reading this, the article "what is the replication method of python collection set and creating objects" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Tags: Object element copy output copy method method content article mode variable address result assignment interpretation input operation proper two intersection representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Huawei Microsoft Apple