Python Learning: List

Source: Internet
Author: User

List

a = [' abc ', ' BCD ', ' CDE ', ' def ', ' EFG ']
print (a)
List of operations: Adding and deleting changes

1) Check: Slice
print (A[1:3]) #从 ' BCD ' taken to ' CDE ', the list value Gu Tou regardless of the tail.
print (a[1:]) #从 ' BCD ' to ' EFG ', from head to tail
print (a[1:-1]) #从 ' BCD ' to ' def ', the list value Gu Tou regardless of the tail.
print (A[1:-1:2]) #2代表步长, the code function is to take values from left to right, value ' BCD ', ' def '
print (A[1::2]) #2代表步长, the code function is to take values from left to right, value ' BCD ', ' def '
print (A[3::-2]) #-2 represents the step size, the code function is to take the value from right to left, value ' def ', ' BCD '
print (a[-2::-1]) #从右边第二个开始取值, take value from ' def ' to ' abc '

2) Increase: increased
Append Add a value to the list at the end
Insert Inserts a value anywhere in the middle of the list
a.append (' fgh ')
print (a)

A.insert (1, ' FGH ')
print (a)

3) Change: Modify
to modify a list slice before assigning a value
a[1] = ' Ghi '
print (a)

A[1:3] = [' A ', ' B ']
print (a)

4) Deleting: deleting
Remove deletes the specified content as a whole and cannot delete slices
Pop Deletes the specified position value and returns the deleted value
del You can delete the contents of the list or delete the entire list or object
a.remove (' abc ')
print (a)

B = A.pop (1)
print (a)
print (b)

del A[0]
print (a)
del a
print (a)

5) Count effect: Calculates the number of occurrences of an element in a list
t = [' A ', ' B ', ' B ', ' A ', ' C ', ' d '].count (' a ')
print (t)

6) Extend effect: Add list to another list
a = [a]
B = [4,5,6]
A.extend (b)
print (a)
print (b)

7) The position of the index output element in the list
a = [' abc ', ' BCD ', ' CDE ', ' def ', ' EFG ']
Print (A.index (' CDE '))

Python Learning: List

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.