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 Pandas 转换unix时间戳方式

Python Pandas 转换unix时间戳方式

使用pandas自带的pd.to_datetime把 unix 时间戳转为时间时默认是转换为 GMT标准时间   北京时间比这个时间还要加 8个小时, 使用pyth...

详解C++编程中一元运算符的重载

可重载的一元运算符如下: !(逻辑“非”) &(取址) ~(二进制反码) *(取消指针引用) +(一元加) -(一元求反) ++(递增) --(递减)...

精确查找PHP WEBSHELL木马的方法(1)

精确查找PHP WEBSHELL木马的方法(1)

先来看下反引号可以成功执行命名的代码片段。代码如下: 复制代码 代码如下: `ls -al`; `ls -al`; echo "sss"; `ls -al`; $sql = "SELEC...

python 计算积分图和haar特征的实例代码

下面的代码通过积分图计算一张图片的一种haar特征的所有可能的值。初步学习图像处理并尝试写代码,如有错误,欢迎指出。 import cv2 import numpy as np im...

浅谈python3.6的tkinter运行问题

python3.6在运行tkinter时要选择 run as Python unit-test, 否则报错 ModuleNotFoundError: No module named 't...