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 are the knowledge points of Python collection

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

Share

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

This article mainly explains "what are the knowledge points of Python collection". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the knowledge points of Python collection"?

I. Collection (1) introduction to Collection

Set is an unordered sequence of non-repeating elements, which is similar to the concept of set in mathematics. In Python, you can use curly braces {} or the set () function to create a collection.

It is important to note that you must use set () instead of {} to create an empty collection, because {} is used to create an empty dictionary.

(2) create a collection

1. Because {} is used to create an empty dictionary. So create an empty collection, which can only be created through set (), as shown below:

A = set ()

2. Create a normal collection and create it directly with curly braces {}, as shown below:

A = {1,2,3,4,5} print (a)

Output result:

{1, 2, 3, 4, 5}

In addition, you can also use set () to create:

B = set ("abc121") print (b)

Output result:

{'baked,' 1percent, '2percent,' clockwise,'a'}

The order of the elements output is different from the order in which it was created. It can be seen that the elements in the collection are unnecessary and cannot be repeated.

(3) set operation

The following is a demonstration of the different operations between two sets:.

A = set ("ac123") b = set ("abc121") print (a) print (b) print ("elements contained in collection a but not in set b:", a-b) print ("all elements contained in collection an or b:", a | b) print ("elements contained in both sets an and b:", a & b) print ("elements not contained in both an and b", a ^ b)

The output is as follows:

Elements contained in collection a but not in set b: all elements contained in collection an or b: all elements contained in set an or b: {'1sectors,' asides, 'cations,' 3' Elements contained in 'baked,' 2'} sets an and b: {'clocked,' 1percent, 'asides,' 2'} elements that are not included in both an and b, basic operations of the set (1) add elements

There are two ways to add new elements to an existing collection:

1. Use set.add (x) to add a new element x to the collection, as demonstrated below:

A = {1,2,3} a.add (6) print (a)

Output result:

{1, 2, 3, 6}

Note: if the element already exists, no action is taken.

2. You can also add elements using the method set.update (x), and the parameters can be lists, tuples, dictionaries, etc., as shown below:

A = {"a", "b", "c"} a.update ({"d", "e"}) print (a)

Output result:

{'baked,' crested, 'dashed,' estranged,'a'}

In addition, the elements added can also be lists, tuples, and so on:

A = {"a", "b", "c"} a.update ((1,2), (3,4)) print (a)

Output result:

{1, 'baked, 2, 3, 4,' cached,'a'} (2) Delete elements

There are three ways to delete elements in a collection:

1. Use the method set.remove (x) to delete element x from the collection, as demonstrated below:

A = {"a", "b", "c"} a.remove ("b") print (a)

Output result:

{'clockwise,' a'}

Note: if the deleted element does not exist, an error will occur.

2. Using set.discard () can also delete elements in the collection, and if the element does not exist, no error will occur. The demonstration is as follows:

A = {"a", "b", "c"} a.discard ("b") print (a)

Output result:

{'averse,' c'}

3. You can also delete an element in the collection randomly through set.pop (), as shown below:

A = {"a", "b", "c"} a.pop () print (a)

Output result:

{'averse,' b'}

Note: because it is deleted randomly, the result of each output may be different.

(3) determine whether the element is in the collection.

Using syntax: x in set, you can determine whether element x is in the collection set, return True if it exists, or False if it doesn't exist. The demonstration is as follows:

Set1 = {"a", "b", "c"} if "b" in set1:print ("b exists in collection") else:print ("b does not exist in collection")

Output result:

B exists in the set. III. Set built-in function

In the Python collection, some built-in functions and methods are provided, which greatly facilitate developers to deal with collection-related data. Some common collection built-in functions are summarized below, as shown in the following table:

Function description set.add (x) adds elements to the collection set.update (x) adds elements (list, tuple, dictionary) set.remove (x) deletes elements in the collection xset.discard (x) deletes elements in the collection xset.pop () randomly deletes an element in the collection set.clear () empties the collection, making it an empty collection set.copy () copies a collection set.intersection (set1, set2... Etc) returns the elements contained in two or more sets, that is, the intersection set.union (set1, set2...) Returns the union of two sets, that is, the element that contains all sets set1.difference (set2) calculates the difference between the set set1 and the set set2

At this point, I believe you have a deeper understanding of "what are the knowledge points of Python collection?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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