python把转列表为集合的方法
作者:Lay_ZRS 时间:2021-06-03 07:28:41
set()函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
set 语法:
class set([iterable])
参数说明:
iterable -- 可迭代对象对象;
返回值:
返回新的集合对象。
将列表转为集合:
list1=[1,3,4,3,2,1]
list1=set(list1)
print(list1)
结果如下:
(1,2,3,4)
扩展举例:
python将3X4的矩阵列表转换为4X3列表
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
# 方法一
# for x in range(len(matrix)):
# print (matrix[x])
# pass
hehe = [[row[i] for row in matrix] for i in range(4)]
print (hehe)
# 方法二
one = []
for x in range(4):
one.append([row[x] for row in matrix])
pass
print (one)
# 方法三
three = []
for x in range(4):
two = []
for i in matrix:
two.append(i[x])
pass
three.append(two)
pass
print (three)
标签:python,列表,集合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
聊聊python中的load、loads实现反序列化的问题
2023-07-20 04:38:52
关于Dreamweaver乱码问题的解决方案
2010-09-02 12:36:00
python主线程与子线程的结束顺序实例解析
2023-10-09 12:46:07
基于php+mysql的期末作业小项目(学生信息管理系统)
2023-06-13 00:39:34
![](https://img.aspxhome.com/file/2023/4/55524_0s.jpg)
tensorflow实现从.ckpt文件中读取任意变量
2023-01-04 15:39:40
PHP实用函数分享之去除多余的0
2023-11-15 02:07:59
python绘制双柱形图代码实例
2022-02-28 19:42:27
![](https://img.aspxhome.com/file/2023/4/97334_0s.png)
Python 函数用法简单示例【定义、参数、返回值、函数嵌套】
2022-01-09 22:58:31
![](https://img.aspxhome.com/file/2023/0/89060_0s.png)
Oracle数据库系统紧急故障处理方法
2010-07-26 12:57:00
解决安装python库时windows error5 报错的问题
2022-06-14 05:26:04
PHP连接MySQL数据库的三种方式实例分析【mysql、mysqli、pdo】
2023-11-15 01:31:13
Oracle常用命令大全集
2010-07-21 13:18:00
不建议使用jquery的情况
2008-03-10 12:28:00
详解python上传文件和字符到PHP服务器
2023-10-15 19:02:12
强悍的Python读取大文件的解决方案
2021-12-26 19:25:28
删除PHP数组中头部、尾部、任意元素的实现代码
2023-06-14 12:40:44
![](https://img.aspxhome.com/file/2023/1/55471_0s.png)
PHP封装的PDO数据库操作类实例
2023-11-18 04:54:31
Symfony2框架创建项目与模板设置实例详解
2023-11-20 23:36:39
![](https://img.aspxhome.com/file/2023/9/92809_0s.jpg)
阿里巴巴中秋节logo设计的艰辛曲折过程
2008-09-11 17:15:00
![](https://img.aspxhome.com/file/UploadPic/20089/11/2008911171924597s.jpg)
PHP完全二叉树定义与实现方法示例
2023-07-04 10:49:10
![](https://img.aspxhome.com/file/2023/8/55378_0s.jpg)