浅谈python中set使用

 更新时间:2016年06月30日 09:29:52   投稿:jingxian  
下面小编就为大家带来一篇浅谈python中set使用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

浅谈python中set使用

In [2]: a = set()  # 常用操作1 
 
In [3]: a 
Out[3]: set() 
 
In [4]: type(a) 
Out[4]: set
 
In [5]: b = set([1, 3]) 
 
In [6]: b 
Out[6]: {1, 3} 
 
In [7]: type(b) 
Out[7]: set
 
In [8]: b.update(2) 
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last) 
<ipython-input-8-d51e2fe4c50a> in <module>() 
----> 1 b.update(2) 
 
TypeError: 'int' object is not iterable 
 
In [9]: b.update({2}) 
 
In [10]: b 
Out[10]: {1, 2, 3} 
 
In [11]: b.update([4]) 
 
In [12]: b 
Out[12]: {1, 2, 3, 4} 
 
In [13]: a.di 
a.difference     a.difference_update a.discard 
 
In [13]: a.dif 
a.difference     a.difference_update 
 
In [13]: a.difference(b) 
Out[13]: set() 
 
In [14]: a 
Out[14]: set() 
 
In [15]: b.difference(a) 
Out[15]: {1, 2, 3, 4} 
 
In [16]:

常用操作2

In [16]: a.add({1, 3}) 
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last) 
<ipython-input-16-98cdf4d0875e> in <module>() 
----> 1 a.add({1, 3}) 
 
TypeError: unhashable type: 'set'
 
In [17]: a.add(4) 
 
In [18]: a 
Out[18]: {4} 
 
In [19]: a.issu 
a.issubset  a.issuperset 
 
In [19]: a.issubset(b) 
Out[19]: True
 
In [20]: a.remove(4) 
 
In [21]: a 
Out[21]: set() 
 
In [22]: a.union(b) 
Out[22]: {1, 2, 3, 4} 
 
In [23]: a 
Out[23]: set() 
 
In [24]: b 
Out[24]: {1, 2, 3, 4} 
 
In [25]: b.pop() 
Out[25]: 1
 
In [26]: a.copy(b) 
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last) 
<ipython-input-26-9e8a5f057ffd> in <module>() 
----> 1 a.copy(b) 
 
TypeError: copy() takes no arguments (1 given) 
 
In [27]: a.copy() 
Out[27]: set() 
 
In [28]: c = a.copy() 
 
In [29]: c 
Out[29]: set() 
 
In [30]: a 
Out[30]: set() 
 
In [31]: a.add({234}) 
---------------------------------------------------------------------------
TypeError                 Traceback (most recent call last) 
<ipython-input-31-6073e02d68a9> in <module>() 
----> 1 a.add({234}) 
 
TypeError: unhashable type: 'set'
 
In [32]: a.add(234) 
 
In [33]: c 
Out[33]: set() 
 
In [34]: a 
Out[34]: {234}

常用操作3

In [35]: a.clear() 
 
In [36]: a 
Out[36]: set() 
 
In [39]: a = {1} 
 
In [40]: b = {1, 2} 
 
In [41]: a.intersection(b) 
Out[41]: {1} 
 
In [43]: a 
Out[43]: {1} 
 
In [44]: b = {1, 2, 3} 
 
In [45]: a.union(b) 
Out[45]: {1, 2, 3} 
 
In [45]: a.union(b) 
Out[45]: {1, 2, 3} 
 
In [46]: a & b 
Out[46]: {1} 
 
In [47]: a ^ b 
Out[47]: {2, 3} 
 
In [48]: a - b 
Out[48]: set() 
 
In [49]: b - a 
Out[49]: {2, 3}<BR><BR> 
?
1
2
3
4
5
6
7
8
9
10
11 In [50]: a > b 
Out[50]: False
 
In [51]: b > a 
Out[51]: True
 
In [52]: a == b 
Out[52]: False
 
In [53]: a != b 
Out[53]: True 

以上这篇浅谈python中set使用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python中无法导入本地安装好的第三方库问题

    python中无法导入本地安装好的第三方库问题

    这篇文章主要介绍了python中无法导入本地安装好的第三方库问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Python合并pdf文件的工具

    Python合并pdf文件的工具

    PDF文件合并工具是非常好用可以把多个pdf文件合并成一个,本文以5个pdf文件为例给大家分享具体操作方法,通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-07-07
  • python opencv摄像头的简单应用

    python opencv摄像头的简单应用

    这篇文章主要为大家详细介绍了python opencv摄像头的简单应用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • 手动安装Anaconda环境变量的实现教程

    手动安装Anaconda环境变量的实现教程

    这篇文章主要介绍了手动安装Anaconda环境变量的实现教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • Python如何设置指定窗口为前台活动窗口

    Python如何设置指定窗口为前台活动窗口

    这篇文章主要介绍了Python如何设置指定窗口为前台活动窗口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • 让 python 命令行也可以自动补全

    让 python 命令行也可以自动补全

    这篇文章主要介绍了让 python 命令行也自动补全,需要的朋友可以参考下
    2014-11-11
  • python 获取文件列表(或是目录例表)

    python 获取文件列表(或是目录例表)

    在python的应用过程中,经常会用到获取文件列表的方法,常规的做法是这样的
    2009-03-03
  • python处理xls文件openpyxl基础操作

    python处理xls文件openpyxl基础操作

    这篇文章主要为大家介绍了python处理xls文件openpyxl基础操作,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Python Pingouin数据统计分析技术探索

    Python Pingouin数据统计分析技术探索

    Pingouin库基于pandas、scipy和statsmodels,为用户提供了执行常见统计分析的功能,它支持各种统计方法和假设检验,例如 t-tests、ANOVA、correlation analysis 等,本文通过一些示例代码,以更全面地了解如何使用Pingouin库进行统计分析,
    2024-01-01
  • tensorflow 实现自定义layer并添加到计算图中

    tensorflow 实现自定义layer并添加到计算图中

    今天小编就为大家分享一篇tensorflow 实现自定义layer并添加到计算图中,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02

最新评论