Quick start of automation -- python(19)--set set

Posted by nicolam1 on Mon, 31 Jan 2022 09:24:02 +0100

This is Qing'an. We learned list, tuple tuple and dict dictionary. In this chapter, we will learn set.

concise

The set set is an unordered "bag" with unique values. A simple collection can contain values of any data type. If there are two sets, you can perform standard set operations such as union, intersection, and set subtraction.

So what exactly is a set?

a_ = {1} 
print(a)
print(type(a_))

After printing, you can see < class' set >, which is a set. To create a set containing only one value, just place the value between curly braces.

a_ = {1, 2}
print(a_)

To create a multivalued collection, separate the values with commas and wrap all the values in curly braces.

List based collection

This is actually a way of converting a list to a collection.

list_ = ['a', 'b', 'c']
print(set(list_))

List after conversion_ Is it just a collection? No. It's still a list. It's the initial variable. The initial value is the list. We didn't change the initial value. Just output conversion.

Empty set

a_ = set()
print(a_)

It's better not to give a value to an empty set. It's better to say members directly than values.

Modify set

add value

add()

list_ = {'a', 'b', 'c'}
list_.add('qingan')
list_1 = {1, 2, 3}
list_1.add(4)
print(list_)
print(list_1)

Here I give two examples, in the form of string, and one is of integer type. Why give two examples.

In the form of string, if it is written as above, the print is out of order. As I said before. Random arrangement, so after add here, the value also appears randomly in one of the four strings, and the print position may be different each time. However, if it is an integer, it will be output in the original form. And write the value of add after it.

update()

list_ = {'a', 'b', 'c'}
list_.update({1, 2, 3})
print(list_)
list_.update({'qingan'}, {'wubieshi'})
print(list_)
list_.update([1, 2, 'qingan'])
print(list_)

Here's the point. I've written three forms of adding values. I'm not asking you to look at disorder here. Look at the update() method.

{'c', 1, 2, 3, 'a', 'b'}
{'c', 1, 2, 3, 'a', 'wubieshi', 'qingan', 'b'}
{'c', 1, 2, 3, 'a', 'wubieshi', 'qingan', 'b'}

The result is this, whether it's multiple sets, a set, or a list. The update() method takes only one collection as a parameter and adds all its members to the initial list. It behaves like calling the add() method on each member in the parameter set. The update() method can also accept some other data type objects as parameters, including lists. If the list is passed in when called, update() will add all the elements in the list to the initial collection.

Note: there is still weight removal here. It doesn't just add values.

Delete value

There are three methods, two of which are also in contact. Specifically:

discard()

# 1
list_ = {1, 3, 5, 7, 9, 11}
list_.discard(7)
print(list_)

discard() takes a single value as a parameter and removes it from the collection. However, if you delete a value that does not exist, it will not report an error, such as list_discard(6).

remove()

# 2
list_ = {1, 3, 5, 7, 9, 11}
list_.remove(7)
print(list_)

The remove() method also takes a single value as an argument and removes it from the collection. If you delete a non-existent value, it will throw an exception and tell you an error, such as list_remove(6).

Traceback (most recent call last):
  File "F:\selenium_demo\shopX_demo\index\book.py", line 12, in <module>
    list_.remove(6)
KeyError: 6

pop()

list_ = {'1', '3', '5', 7, '9', 11}
list_.pop()
print(list_)
The pop() method deletes a value from the collection and returns it. However, due to Collections are unordered and have no " the last one " Value, so deletion cannot be controlled Which value is divided by. It's basically random. That's why we don't use integers for demonstration here. Don't get confused. Run this method yourself to see the results.

clear()

list_ = {'1', '3', '5', 7, '9', 11}
list_.clear()
print(list_)

The clear() method deletes all the values in the collection, leaving an empty collection. It is equivalent to a_set = set(), which creates a new empty set and overwrites a_ The previous value of the set variable.

If you run pop() after clear() or other methods, you will throw an exception.

Common collection operations

Does it exist in the collection

list_ = {1, 3, 5, 7, 9, 11}
i = 7 in list_
print(i)

This usage is still very common to judge whether 7 exists in the list_ In the collection. Return TRUE OR FALSE

union() Union

list_ = {1, 3, 5, 7, 9, 11}
list_1 = {2, 4, 6, 8, 10, 12}
o = list_.union(list_1)
print(o)

Organize the two sets into a new set and remove the duplication!

Intersection

list_ = {1, 3, 5, 7, 9, 11}
list_1 = {1, 2, 4, 6, 8, 10, 12}
i = list_.intersection(list_1)
print(i)

The intersection() method returns a new set containing all elements that appear "simultaneously" in both sets.

difference() difference

list_ = {1, 3, 5, 7, 9, 11}
list_1 = {1, 3, 4, 6, 8, 10, 12}
i = list_.difference(list_1)
print(i)

In the new collection returned by the difference() method, the list is output_ List in_ There is no value in 1.

symmetric_difference()

list_ = {1, 3, 5, 7, 9, 11}
list_1 = {1, 3, 4, 6, 8, 10, 12}
i = list_.symmetric_difference(list_1)
print(i)

        symmetric_ The difference () method returns a new collection containing all the elements that appear in only one of the collections. It is a bit similar to removing the duplicate values in the two sets.

Topics: Python