set python用法_Python set()用法及代码示例
集合(Set)是由不同语言组成的序列的数学术语,Python也对其语言进行了扩展,并且可以使用set()轻松制作。
set()方法用于将任何可迭代元素转换为可迭代元素的不同元素和排序序列,通常称为Set。
用法: set(iterable)
参数:任何可重复的序列,例如列表,元组或字典。
返回:如果未传递任何元素,则为空集。排序的非重复元素可迭代修改为作为参数传递。
代码1:使用列表和元组演示set()
# Python3 code to demonstrate the
# working of set() on list and tuple
# initializing list
lis1 = [ 3, 4, 1, 4, 5 ]
# initializing tuple
tup1 = (3, 4, 1, 4, 5)
# Printing iterables before conversion
print("The list before conversion is:" + str(lis1))
print("The tuple before conversion is:" + str(tup1))
# Iterables after conversion are
# notice distinct and sorted elements
print("The list after conversion is:" + str(set(lis1)))
print("The tuple after conversion is:" + str(set(tup1)))
输出:
The list before conversion is:[3, 4, 1, 4, 5]
The tuple before conversion is:(3, 4, 1, 4, 5)
The list after conversion is:{1, 3, 4, 5}
The tuple after conversion is:{1, 3, 4, 5}
set()的属性
没有传递参数来创建空集
也可以使用set创建字典,但是转换后仅保留键,值丢失。
代码2:在字典上演示集合的工作
# Python3 code to demonstrate the
# working of set() on dictionary
# initializing list
dic1 = { 4 :'geeks', 1 :'for', 3 :'geeks' }
# Printing dictionary before conversion
# internaly sorted
print("Dictionary before conversion is:" + str(dic1))
# Dictionary after conversion are
# notice lost keys
print("Dictionary afer conversion is:" + str(set(dic1)))
输出:
Dictionary before conversion is:{1:'for', 3:'geeks', 4:'geeks'}
Dictionary afer conversion is:{1, 3, 4}
set python用法_Python set()用法及代码示例相关推荐
- python end用法_Python turtle.end_fill方法代码示例
本文整理汇总了Python中turtle.end_fill方法的典型用法代码示例.如果您正苦于以下问题:Python turtle.end_fill方法的具体用法?Python turtle.end_ ...
- python中stringvar的用法_Python tkinter.StringVar方法代码示例
本文整理汇总了Python中tkinter.StringVar方法的典型用法代码示例.如果您正苦于以下问题:Python tkinter.StringVar方法的具体用法?Python tkinter ...
- python中formatter的用法_Python pyplot.FuncFormatter方法代码示例
本文整理汇总了Python中matplotlib.pyplot.FuncFormatter方法的典型用法代码示例.如果您正苦于以下问题:Python pyplot.FuncFormatter方法的具体 ...
- python geometry用法_Python geometry.MultiPolygon方法代码示例
本文整理汇总了Python中shapely.geometry.MultiPolygon方法的典型用法代码示例.如果您正苦于以下问题:Python geometry.MultiPolygon方法的具体用 ...
- python中geometry用法_Python geometry.Point方法代码示例
本文整理汇总了Python中shapely.geometry.Point方法的典型用法代码示例.如果您正苦于以下问题:Python geometry.Point方法的具体用法?Python geome ...
- python中bind的用法_Python socket.bind方法代码示例
本文整理汇总了Python中socket.bind方法的典型用法代码示例.如果您正苦于以下问题:Python socket.bind方法的具体用法?Python socket.bind怎么用?Pyth ...
- python中font的用法_Python font.nametofont方法代码示例
本文整理汇总了Python中tkinter.font.nametofont方法的典型用法代码示例.如果您正苦于以下问题:Python font.nametofont方法的具体用法?Python fon ...
- python中fact用法_Python covariance.EllipticEnvelope方法代码示例
本文整理汇总了Python中sklearn.covariance.EllipticEnvelope方法的典型用法代码示例.如果您正苦于以下问题:Python covariance.EllipticEn ...
- doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例
本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...
- g的python实现_Python flask.g方法代码示例
本文整理汇总了Python中flask.g方法的典型用法代码示例.如果您正苦于以下问题:Python flask.g方法的具体用法?Python flask.g怎么用?Python flask.g使用 ...
最新文章
- IntellJ_打开选中的文件所在的文件夹
- Spring Boot + Vue + Shiro 实现前后端分离、权限控制
- 敏捷开发和测试中重现缺陷和验证缺陷的解决方案(2)
- thinkphp学习笔记7—多层MVC
- 忍不住还是装了一下Windows Vista
- 并发编程6 锁进程队列
- Spring-AOP @AspectJ切点函数之target()和this()
- [十三]JavaIO之PushBackInputStream
- 岗位推荐 | 阿里巴巴达摩院招聘自然语言处理、机器翻译算法专家
- pycharm替换和查找文件中所有相同代码方法
- 高通侧目!联发科发布面向高端手机的5G芯片
- excel导入数据到mysql数据库
- 怎么用diskgenius扩大c盘,超简单。
- ollydbg使用心得
- 分布式深度强化学习的内功修炼之隐式分布
- FileZilla 服务器 报Warning: FTP over TLS is not enabled, users cannot securely log in.
- VUE大屏可视化实战
- 执行 this.$destroy()后原生DOM事件也无法执行
- matlab作业 阳光的快乐老爹,霍思燕6岁儿子近照曝光,调皮起来超阳光,完美继承老爹容颜!...
- 使用Encoder-Decoder模型自动生成对联的思路
热门文章
- MYSQL-创建存储过程
- PCM音频文件的制作
- 史无前例!iPhone 14 Pro曝光:首次8GB RAM
- 疑似Redmi K40S工信部入网:主打双十一 最高支持120W快充
- 华为:预计6G将于2030年左右投向市场
- 韩国浦项化学在中国斥资超2800亿韩元投建电动车电池材料厂
- 不到6年“拼”活近8亿用户 拼多多还在狂奔
- 群辉发布RackStation系列机型——RS3621RPxs、RS3621xs+与RS4021xs+
- 取代Win10 消息称Windows 10X将在明年推出
- 苦等8个月!华为最令人期待的手机终于要来了:最快月底开卖