python把转列表为集合的方法

yipeiwu_com7年前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 thrift搭建服务端和客户端测试程序

本文生动简洁介绍了如何通过python搭建一个服务端和客户端的简单测试程序。 一、简介 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码...

Python运行报错UnicodeDecodeError的解决方法

Python2.7在Windows上有一个bug,运行报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in...

numpy数组做图片拼接的实现(concatenate、vstack、hstack)

两种方法拼接 #img = np.vstack((img, img2)) # vstack按垂直方向,hstack按水平方向 img = np.concatenate((img,...

Python正则表达式匹配ip地址实例

本文实例讲述了正则表达式匹配ip地址实例。代码结构非常简单易懂。分享给大家供大家参考。 主要实现代码如下: import re reip = re.compile(r'(?&...

Python线程池模块ThreadPoolExecutor用法分析

本文实例讲述了Python线程池模块ThreadPoolExecutor用法。分享给大家供大家参考,具体如下: python3内置的有Threadingpool和ThreadPoolEx...