python把转列表为集合的方法
set()函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
set语法:
classset([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], ] #方法一 #forxinrange(len(matrix)): # print(matrix[x]) # pass hehe=[[row[i]forrowinmatrix]foriinrange(4)] print(hehe) #方法二 one=[] forxinrange(4): one.append([row[x]forrowinmatrix]) pass print(one) #方法三 three=[] forxinrange(4): two=[] foriinmatrix: two.append(i[x]) pass three.append(two) pass print(three)
以上就是本次关于python怎么把转列表为集合的详细内容,感谢大家的学习和对毛票票的支持。