How python creates collections
This article mainly introduces how to create a collection of python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The method of creating a collection
Create a collection through the set function, or you can create it using curly braces with values. Such as {1, 2}, but cannot be created with empty curly braces. (the empty curly braces are an empty dictionary)
Examples are as follows:
Test_set_01 = set () #-> an empty collection test_set_02 = set ([1,2,3]) #-> incoming list or tuple test_set_03 = {1,2 3} #-> passing in the element test_set_04 = {} #-> this way is wrong This is an empty dictionary print (type (test_set_04)) # the result is as follows: #-- > test_list_01 = ['name',' age', 'birthday'] test_set_01 = set (test_list_01) print (test_set_01) # the result is as follows: # > {' name', 'age',' birthday'} you can see that the list is not passed in Instead, the element of the list test_list_02 = ['name',' age', 'birthday',' age'] test_set_02 = set (test_list_02) print (test_set_02) # the execution result is as follows: # > {'name',' age', 'birthday'} you can see the duplicate elements in the list Test_list_03 = (1, 2, 3, 1, 5) test_set_03 = set (test_list_03) print (test_set_03) # the result is as follows: # > > {1, 2, 3, 5} you can see the repeated elements in the tuple Deduplicated processing test_set_04 = {['name',' age', 'birthday']} print (test_set_04) # the execution result is as follows: # > TypeError: unhashable type:' non-transferable list within the list' collection Otherwise, an error will be reported: test_set_05 = {{'name',' age', 'birthday'}} print (test_set_05) # execution result is as follows: # > TypeError: unhashable type:' the dictionary test_set_06 = {'name', 1,3.14,666,888} print (test_set_06) # is also not allowed in the set' collection: # > {' name', 3.14,1, (666) 888)} different data types can be passed into the collection (the list cannot be passed directly). Thank you for reading this article carefully. I hope the article "how to create a collection of python" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!