Python Learning Notes (4) collections, tuples, dictionaries, collections

Source: Internet
Author: User

List

Create a list

1. Create directly

     >>> a = [1, ' 1 ', ' 1.0 ']
>>> A
[1, ' 1 ', ' 1.0 ']

    2. Create with List

>>> A = list ([1, ' 1 ', ' 1.0 ']) >>> A [1, ' 1 ', ' 1.0 '] #括号内可以用小括号, brackets, curly braces.

Method

1. Number of elements in count statistics list

>>> a

[1, 2, 3, 1, 1, 2, 3]
>>> A.count (1)
3

   2. Index finds the location of the list where the element is located (the same element in the list returns the first position)

    >>> A

[1, 2, 3, 1, 1, 2, 3]

>>> A.index (1)

0
>>> A.index (2)
1

3. The X in list determines if there are elements in the table, returning True or false

>>> 1 in a

True

>>> 5 in a

False

4. Append add elements to the end of list

     >>> a = list ([1,2,3,1,1,2,3])

>>> A.append (4)
>>> A
[1, 2, 3, 1, 1, 2, 3, 4]

5. Insert add element at specified position in list

>>> a.insert (0, 5)
>>> A
[5, 1, 2, 3, 1, 1, 2, 3, 4]

6. Extends () adds the value of the B list after the A list

  >>> B = [5, 6, 7]

>>> A.extend (b)

>>> A
[5, 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]

7. Modifying list values

>>> a[0] = ' 5 '

     >>> A

[' 5 ', 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]

8. Delete list values

Pop () Delete the element and return the deleted value by default delete the last element, you can specify a location to delete

      >>> A.pop ()

7

>>> A.pop (0)

' 5 '

Remove () deletes the specified element when there are multiple identical elements when the first one in the list is deleted

      >>> A

      [1, 2, 3, 1, 1, 2, 3, 4, 5, 6]

>>> A.remove (3)
>>> A
[1, 2, 1, 1, 2, 3, 4, 5, 6]

       Del Specify location delete element or delete list

      >>> A

[1, 2, 1, 1, 2, 4, 5, 6]
>>> del A[1]
>>> A
[1, 1, 1, 2, 4, 5, 6]

     >>> del a

>>> A
Traceback (most recent):
File "<pyshell#49>", line 1, in <module>
A
Nameerror:name ' A ' is not defined

Clear () empties the elements in the list

' >>> a

[1, 1, 1, 2, 4, 5, 6]
>>> A.clear ()
>>> A
[]

      

        

      

      

      

      

      

    

    

    

      

Python Learning Notes (4) collections, tuples, dictionaries, collections

Related Article

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.