python Method for Turning a Turn List into a Set

  • 2021-07-03 00:39:49
  • OfStack

The set () function creates an unordered non-repeating element set, which can be used for relational testing, deleting duplicate data, and calculating intersection, difference set, union set and so on.

set syntax:

class set([iterable])

Parameter description:

iterable--Iterable Object Object;

Return value:

Returns a new collection object.

Convert a list into a collection:


list1=[1,3,4,3,2,1]

list1=set(list1)

print(list1)

The results are as follows:


 ( 1 , 2,3,4 ) 

Examples of extensions:

python converts the matrix list of 3X4 into a list of 4X3


matrix = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
]

#  Method 1
# for x in range(len(matrix)):
# 	print (matrix[x])
# 	pass
hehe = [[row[i] for row in matrix] for i in range(4)]
print (hehe)
#  Method 2
one = []
for x in range(4):
	one.append([row[x] for row in matrix])
	pass
print (one)

#  Method 3
three = []
for x in range(4):
	two = []
	for i in matrix:
		two.append(i[x])
		pass
	three.append(two)
	pass
print (three)

The above is the details about python how to turn the list into a collection. Thank you for your study and support for this site.


Related articles: