Ways to List Python

Source: Internet
Author: User

# last new element in list
Li = [11, 22, 33]
Print (LI)
Li.append # Add an element to the original list last
Print (LI)
EXECUTE as follows:
[11, 22, 33]
[11, 22, 33, 44]
# empty List
Li = [11, 22, 33, 44, 55]
Li.clear () # empty list
Print (LI)
EXECUTE as follows: []
# Copy of List
Li = [22, 21, 32, 53]
bi = li.copy () # Copy a new list
Print (LI)
Print (BI)
EXECUTE as follows:
[22, 21, 32, 53]
[22, 21, 32, 53]

# Extend the existing list
Li = [23, 34, 25, 26]
Li.extend ("11") # expands two strings to the list
Print (LI)

[23, 34, 25, 26, ' 1 ', ' 1 ']

Li.extend ([93]) # Extend a list
Print (LI)

[23, 34, 25, 26, ' 1 ', ' 1 ', 93]

BI = [12, 423, 231]
Li.extend (BI) # expands another list to be added from behind the original list
Print (LI)

[23, 34, 25, 26, ' 1 ', ' 1 ', 93, 12, 423, 231]

Li = List ([1, 2, 3])
Print (LI)
Li.extend ([11, 22]) # Extend a list
Print (LI)
Li.extend ((911, 22)) # can also be a tuple
Print (LI)
EXECUTE as follows:

[1, 2, 3]
[1, 2, 3, 11, 22]
[1, 2, 3, 11, 22, 911, 22]

#  List Index
Li = [1, 2, 3, 4, 5]
Print (Li.index (5)) # Gets the lower label of the list element

EXECUTE as follows:
4

# list Insert
Li = [2, 2334, 11, 33]
Li.insert (2, ("A", "B", "C") # Inserting a tuple at index 2
Print (LI)

[2, 2334, (' A ', ' B ', ' C '), 11, 33]

Li.insert (4, [[+]) # Insert a new list at index 4
Print (LI)

[2, 2334, (' A ', ' B ', ' C '), 11, [66, 33], 33]

Li.insert (1, "CC") # Inserts an element at index 1
Print (LI)
[2, ' cc ', 2334, (' A ', ' B ', ' C '), 11, [66, 33], 33]


# When you remove a list element, it is used in the message queue
Li = [2, 2334, 11, 33]
ret = Li.pop () # Gets the value rejected, the last value is deleted by default
Print (LI)

[2, 2334, 11]

Print (ret)

33

Ret1 = Li.pop (0) # Delete the element at the index and get the deleted value
Print (LI)

[2334, 11]

Print (RET1)

2


# Delete the value specified by the list
Li = [11, 22, 11, 22, 11]
Print (LI)
Li.remove (11) # Delete the list of an element, where it is non-indexed, and if there are duplicates delete only the first
Print (LI)

[11, 22, 11, 22, 11]
[22, 11, 22, 11]


# list Element inversion
Li = [11, 22, 33, 44, 55]
Print (LI)
Li.reverse () # Invert list elements
Print (LI)

[11, 22, 33, 44, 55]
[55, 44, 33, 22, 11]
# count the number of elements in a list
Li = [23, 342, 22, 42, 22]
CU = Li.count ($) # counts the number of elements in a list
Print (CU)

2

#列表数组排序
Li = [22, 12, 212, 13, 423]
Li.sort ()
Print (LI)
[12, 13, 22, 212, 423]

 #如果需要一个排序好的副本,同时保持原有列表不变 a = [ 4 , 6 , 2 , 1 , 7 , 9 ] b = a[ : ] b.sort() print b #[1, 2, 4, 6, 7, 9] print a #[4, 6, 2, 1, 7, 9] #注意: b = a[:] Copy all the elements of List A to B through a shard operation, and if you simply assign a value to B:b = A, a or a to the same list, and do not produce a new copy




Ways to List Python

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.