以下是常见的 Python 列表操作方法。
在 Python 中,列表是按特定顺序排列的项目的集合。你可以将任何内容放入列表中。
按照惯例,你通常会将列表名称设为复数。例如,人员列表可以命名为人员。
Python 使用方括号 [] 表示列表,各个元素用逗号分隔。
列表以零为基数。这意味着第一个位置是 0,第二个位置是 1,等等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   |  fruits = ['orange', 'apple', 'melon']
 
  fruits[0]   fruits[-1] 
 
  fruits.append('banana')
 
  fruits.insert(1, 'banana') 
 
  nb_items = len(fruits) 
 
  del fruits[1]   
 
  lastFruit =  fruits.pop()   
 
  fruits = fruits[1:3] fruits[:3]   fruits[2:]   copy_fruits = fruits[:] 
 
  colors = 'red, green, blue'.split(', ')
 
  colors.reverse()
 
  color1 = ['red', 'blue'] color2 = ['green', 'yellow'] color3 = color1 + color2
 
  color3 = [*color1, *color2]
 
  name, price = ['iPhone', 599]
 
  colors = ('red', 'green', 'blue')
 
  colors.sort()  colors.sort(reverse=True)  colors.sort(key=lambda color: len(color)) 
 
  for color in colors:   print(color)
 
  numbers = List(range(1, 10)) 
 
 
 
  fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newFruits = []
  for x in fruits:   if "a" in x:     newFruits.append(x)
  print(newFruits)
 
  fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
  newFruits = [x for x in fruits if "a" in x]
 
  print(newFruits)
 
  | 
 
      
     
    
      
  
  
    
      
      
        
        致力于网站建设与Web开发。喜欢新事物,关注前后端动态,对新的技术有追求, 做一个优秀的web全栈工程师。