Python data type --- list 3, python Data Type

Source: Internet
Author: User

Python data type --- list 3, python Data Type

1. determine whether an element exists in the list: "in" Usage

List = ['frank', 99, 'is ', 78, 4, 'smart'] print (99 in list) print (9 in list) # running result TrueFalse

2. Statistics on the number of elements: count

>>> name = ['3',4,45,4343,34,56,6,7,5,3,9]>>> name.count(9)1>>> name.count(3)1>>>

3. Element Location index: index (
L. index (value, [start, [stop])-> integer -- return first index of value.
Raises ValueError if the value is not present .)

List = ['frank', 99, 'is', 78, 9,939, 4, 4, 'smart', 9] print (99 in list) print (9 in list) # count use num_of_ele = list. count (9) # Use posistion_of_ele = list for index. index (9) print ("[% s] 9 in list, posistion is [% s]" % (num_of_ele, posistion_of_ele) # Test Result TrueTrue [2] 9 in list, posistion is [8]

4. List Extension: extend

Name = ["frank", "Malon", "Lee"] name2 = ["Andy", "Troy"] name. extend (name2) print (name) print (name2) # Run the result ['frank', 'malon', 'Lee ', 'andy ', 'troy'] ['andy ', 'troy']

5. copy the list: copy # L. copy ()-> list -- a shallow copy of L only for superficial copying

Name = ['Alex ', 'rain', 'erik', ['A', 'B', 'C'], 'frank'] name3 = name. copy () print ("Test 1") print (name) print (name3) print ("Test 2") name [0] = 'Alex 'print (name) print (name3) print ("Test 3") name [3] [0] = 'aaaaa' print (name) print (name3) print ("Test 4 ") name3 [3] [2] = 'cccccc' print (name) print (name3) # Test Result 1 ['Alex ', 'rain', 'erik ', ['A', 'B', 'C'], 'frank'] ['Alex ', 'rain', 'erik', ['A', 'B ', 'C'], 'frank'] Test 2 ['Alex ', 'rain', 'erik', ['A', 'B', 'C'], 'frank'] ['Alex ', 'rain', 'erik', ['A', 'B', 'C'], 'frank'] Test 3 ['Alex ', 'rain', 'erik', ['aaaaaaa', 'B', 'C'], 'frank'] ['Alex ', 'rain', 'erik', ['aaaaa', 'B', 'C'], 'frank'] Test 4 ['Alex ', 'rain', 'erik', ['aaaaaaa', 'B', 'cccccccccccc'], 'Frank '] ['Alex', 'rain', 'erik ', ['aaaaa',' B ', 'cccccccc'], 'frank']

6. List copy --- deepcopy (import copy)

Import copyname = ['Alex ', 'rain', 'erik', ['A', 'B', 'C'], 'frank'] name3 = name. copy () name4 = copy. deepcopy (name) print ("Test 1") print (name) print (name3) print (name4) name [3] [1] = 'aaaaaaaaa' print ("Test 2") print (name) print (name3) print (name4) # Test Result 1 ['Alex ', 'rain', 'erik ', ['A',' B ', 'C'], 'frank'] ['Alex', 'rain', 'erik ', ['A', 'B', 'C'], 'frank'] ['Alex ', 'rain', 'erik', ['A', 'B ', 'C'], 'frank'] Test 2 ['Alex ', 'rain', 'erik', ['A', 'aaaaaaa', 'C'], 'Frank '] ['Alex', 'rain', 'erik ', ['A', 'aaaaaaaaa', 'C'], 'frank'] ['Alex ', 'rain', 'erik ', ['A',' B ', 'C'], 'frank']

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.