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设计】的支持。

相关文章

使用python turtle画高达

使用python turtle画高达

我就废话不多说了,直接上代码吧! import turtle t=turtle.Turtle() turtle.Turtle().screen.delay(0) tleft=turt...

Python 登录网站详解及实例

Python 登录网站详解及实例 对于大部分论坛,我们想要抓取其中的帖子分析,首先需要登录,否则无法查看。   这是因为 HTTP 协议是一个无状态(Stateless)的协议,服务器如...

python 简易计算器程序,代码就几行

代码: 复制代码 代码如下: import os while True: dynamic = input('输入计算表达式:') if dynamic != 'cls': try: re...

解决Python找不到ssl模块问题 No module named _ssl的方法

python安装完毕后,提示找不到ssl模块: [www@pythontab.com ~]$ python Python 2.7.15 (default, Oct 23 2018,...

Python调用C语言的实现

Python中的ctypes模块可能是Python调用C方法中最简单的一种。ctypes模块提供了和C语言兼容的数据类型和函数来加载dll文件,因此在调用时不需对源文件做任何的修改。也正...