当前位置:网站首页>Working with collections
Working with collections
2022-06-24 15:16:00 【User 8442333】
Python The set in is consistent with the set in mathematics , Duplicate elements are not allowed , And it can intersect 、 Combine 、 Subtraction and so on .
def main():
set1 = {1, 2, 3, 3, 3, 2}
print(set1)
print('Length =', len(set1))
set2 = set(range(1, 10))
print(set2)
set1.add(4)
set1.add(5)
set2.update([11, 12])
print(set1)
print(set2)
set2.discard(5)
# remove If the element does not exist, it will trigger KeyError
if 4 in set2:
set2.remove(4)
print(set2)
# Traverse the collection container
for elem in set2:
print(elem ** 2, end=' ')
print()
# Convert tuples into sets
set3 = set((1, 2, 3, 3, 2, 1))
print(set3.pop())
print(set3)
# Intersection of sets 、 Combine 、 Difference set 、 Symmetric difference operation
print(set1 & set2)
# print(set1.intersection(set2))
print(set1 | set2)
# print(set1.union(set2))
print(set1 - set2)
# print(set1.difference(set2))
print(set1 ^ set2)
# print(set1.symmetric_difference(set2))
# Judging subsets and supersets
print(set2 <= set1)
# print(set2.issubset(set1))
print(set3 <= set1)
# print(set3.issubset(set1))
print(set1 >= set2)
# print(set1.issuperset(set2))
print(set1 >= set3)
# print(set1.issuperset(set3))
if __name__ == '__main__':
main()explain :Python Allows some special methods to customize operators for certain types or data structures ( Later chapters will cover ), In the above code, we can call the method of the collection object when we operate on the collection , You can also use the corresponding operator directly , for example
&Operator with intersection The function of the method is the same , But using operators makes the code more intuitive .
边栏推荐
- openinstall携手书链:助力渠道数据分析,共创书联网时代
- Bert whitening vector dimension reduction and its application
- A common defect management tool - Zen, which teaches you from installation to using the handle
- CVPR 2022 - Interpretation of selected papers of meituan technical team
- Restoring to an earlier version in CVS
- How to achieve long-term development of voice social source code?
- postgresql之词法分析简介
- 50 practical applications of R language (23) - important concepts of Bayesian Theory: credibility, model models, and parameters
- How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
- Stm32f1 and stm32cubeide programming examples -ws2812b full color LED driver (based on spi+dma)
猜你喜欢

Method after charging the idea plug-in material theme UI
Redis consistency hash and hash slot

Overview of SAP marketing cloud functions (IV)

As a developer, what is the most influential book for you?

In the eyes of the universe, how to correctly care about counting East and West?

在宇宙的眼眸下,如何正确地关心东数西算?

API data interface for announcement of Hong Kong listed companies

入行 4 年,跳槽 2 次,我摸透了软件测试这一行

Go language concurrency model mpg model

左手代码,右手开源,开源路上的一份子
随机推荐
laravel8使用faker调用工厂填充数据
Laravel 8 realizes auth login
手机注册股票开户 炒股开户安全吗
探索云原生数据库,纵观未来科技发展
Golang实现Biginteger大数计算
SF express: please sign for MySQL soul ten
Multimeter resistance measurement diagram and precautions
ESP32系列--ESP32各个系列对比
Go language concurrency model mpg model
IList of PostgreSQL
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
实战 | 记一次曲折的钓鱼溯源反制
在宇宙的眼眸下,如何正确地关心东数西算?
MySQL log management, backup and recovery
Stm32f1 and stm32cubeide programming examples -ws2812b full color LED driver (based on spi+dma)
Domestic payment system and payment background construction
Low fidelity prototype vs high fidelity prototype, which one is more suitable for your design?
[bitbear story collection] June MVP hero story | technology practice collision realm thinking
FPGA based analog I ² C protocol system design (Part I)
laravel 8 实现Auth登录