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

相关文章

tensorflow 恢复指定层与不同层指定不同学习率的方法

如下所示: #tensorflow 中从ckpt文件中恢复指定的层或将指定的层不进行恢复: #tensorflow 中不同的layer指定不同的学习率 with tf.Graph...

python使用matplotlib绘制雷达图

python使用matplotlib绘制雷达图

本文实例为大家分享了python使用matplotlib绘制雷达图的具体代码,供大家参考,具体内容如下 示例代码: # encoding: utf-8 import pandas...

在python中将字符串转为json对象并取值的方法

如下所示: string =" { "status": "error", "messages": ["Could not find resource or operation '...

解决Python print 输出文本显示 gbk 编码错误问题

前阵子想爬点东西,结果一直提示错误UnicodeEncodeError: ‘gbk' codec can't encode character ‘\xa0' in position,在网...

Python语法分析之字符串格式化

前序 There should be one - and preferably only one - obvious way to do it. ———— the Zen of Pyt...