Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 252

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report