python List common method

= names [ "A", 'B', 'C'] 

# added
names.append ( 'D')
Print (names)
# [ 'A', 'B', 'C', 'D']

# inserted
names .insert (. 1, 'E')
Print (names)
# [ 'A', 'E', 'B', 'C', 'D']

# delete a specific element A
names.remove ( 'A')
Print ( names)
# [ 'E', 'B', 'C', 'D']

# another deletion method:
del names [-1]
Print (names)
# [ 'E', 'B', 'C' ]

# pop delete the default pop the last element, and this element is stored in the pop in
pop = names.pop ()
print ( 'pop =', pop, sep = '')
# pop = C

# Gets the element index
print (names.index ( 'B'))
#. 1

Print (names)
# [ 'E', 'B']

# list reverse arrangement
names.reverse ()
Print (names)
# [ 'B', 'E']

# List Sort: reserve = True indicates descending
names.sort (Reverse = True)
Print (names)
# [ 'E', 'B']

names2 = [1,2,3,4,5]

# merge two list
names.extend (names2)
Print (names)
# [ 'E', 'B',. 1, 2,. 3,. 4,. 5]

# steps print
print (names [0: -1: 2]) # print (names [2} ::
# [ 'E',. 1,. 3]

# list generator
List2 = [X * Range. 3 for X in (10)]
Print (List2)
# [0,. 3,. 6,. 9, 12 is, 15 , 18, 21, 24, 27]

Guess you like

Origin www.cnblogs.com/wztshine/p/11759297.html