Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the non-repeatable data set Set in python

Shulou Source: shulou.com Published: 2022-06-01 17:40:20 09月22日 Update

How to understand the non-repeatable data set in python, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

If you want to keep data from repeating, you need to use Set.

Note: Set initialization is contained with "{}", List initialization is contained with "[]".

provinces = {'Zhejiang',' Jiangsu','Shanghai',' Shanghai'}

When initializing provinces, there are two "Shanghai". When displayed, there is only one "Shanghai", indicating that the Set will be automatically de-weighted. There is also a point. Careful friends may have discovered that the order of elements in provinces has changed, not the order of initialization, indicating that the Set is disordered.

You can use in to determine if an element is in a collection:

Returns "True" if it is in the collection, returns "False" if it is not in the collection.

There is also a quick way to create collections:

The set() method lets you break a string into sets alphabetically.

It is also possible to perform operations between sets:

a-b denotes a set whose elements are in a but not in b

a |b represents the set of elements in a or b

a & b represents a collection of elements in a and in b

a ^ b denotes a set of elements only in a or only in b

Set cannot contain duplicate elements, and is unordered, and sets can be "-","|","&","^"operation Oh.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Tags: Element time Shanghai data method order help calculation clear careful two between content only letters characters strings this articles novices Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MariaDB Linux Redmi NVidia