python把转列表为集合的方法

yipeiwu_com6年前Python基础

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怎么把转列表为集合的详细内容,感谢大家的学习和对【听图阁-专注于Python设计】的支持。

相关文章

Scrapy的简单使用教程

Scrapy的简单使用教程

在这篇入门教程中,我们假定你已经安装了python。如果你还没有安装,那么请参考安装指南。 首先第一步:进入开发环境,workon article_spider 进入这个环境:...

python用ConfigObj读写配置文件的实现代码

发现一个简单而又强大的读写配置文件的lib,http://www.voidspace.org.uk/python/configobj.html。个人觉得最大的亮点在于自带的格式校验功能,...

Python使用gluon/mxnet模块实现的mnist手写数字识别功能完整示例

本文实例讲述了Python使用gluon/mxnet模块实现的mnist手写数字识别功能。分享给大家供大家参考,具体如下: import gluonbook as gb from m...

PyCharm更改字体和界面样式的方法步骤

PyCharm更改字体和界面样式的方法步骤

更改主题 File → Settings → Appearance & Behavior → Appearance → Theme 结果: 更改字体大小 File → Sett...

探索Python3.4中新引入的asyncio模块

使用 Simple Protocol asyncio.BaseProtocol 类是asyncio模块中协议接口(protocol interface)的一个常见的基类。asyncio....