list = [1,2,3,4,5,6,7] print(list[0]) print(list[1]) print(list[2]) print(list[3]) # Slicing the elements print(list[0:6]) # By default the index value is 0 so its starts from the 0th element and go for index -1. print(list[:]) print(list[2:5]) print(list[1:6:2])
list = [1, 2, 3, 4, 5, 6] print(list) # It will assign value to the value to the second index list[2] = 10 print(list) # Adding multiple-element list[1:3] = [89, 78] print(list) # It will add value at the end of the list list[-1] = 25 print(list)
也可以使用 del 关键字删除列表元素。如果我们不知道要从列表中删除哪个元素,Python 还为我们提供了 remove() 方法。
考虑以下示例来删除列表元素。
list = [1, 2, 3, 4, 5, 6] print(list) # It will assign value to the value to second index list[2] = 10 print(list) # Adding multiple element list[1:3] = [89, 78] print(list) # It will add value at the end of the list list[-1] = 25 print(list)
# repetition of list # declaring the list list1 = [12, 14, 16, 18, 20] # repetition operator * l = list1 * 2 print(l)
输出:
[12, 14, 16, 18, 20, 12, 14, 16, 18, 20]
2. Concatenation(串联)
# concatenation of two lists # declaring the lists list1 = [12, 14, 16, 18, 20] list2 = [9, 10, 32, 54, 86] # concatenation operator + l = list1 + list2 print(l)
输出:
[12, 14, 16, 18, 20, 9, 10, 32, 54, 86]
3. Length
# size of the list # declaring the list list1 = [12, 14, 16, 18, 20, 23, 27, 39, 40] # finding length of the list len(list1)
输出:
9
4. Iteration
# iteration of the list # declaring the list list1 = [12, 14, 16, 39, 40] # iterating for i in list1: print(i)
输出:
12 14 16 39 40
5. Membership
# membership of the list # declaring the list list1 = [100, 200, 300, 400, 500] # true will be printed if value exists # and false if not print(600in list1) print(700in list1) print(1040in list1) print(300in list1) print(100in list1) print(500in list1)
输出:
False False False True True True
迭代列表
可以通过使用 for - in 循环来迭代列表。包含四个字符串的简单列表,可以如下迭代。
list = ["John", "David", "James", "Jonathan"] for i inlist: # The i variable will iterate over the elements of the List and contains each element in each iteration. print(i)
#Declaring the empty list l =[] #Number of elements will be entered by the user n = int(input("Enter the number of elements in the list:")) # for loop to take the input for i inrange(0,n): # The input is taken from the user and added to the list as the item l.append(input("Enter the item:")) print("printing the list items..") # traversal loop to print the list items for i in l: print(i, end = " ")
输出:
Enter the number of elements in the list:5 Enter the item:25 Enter the item:46 Enter the item:12 Enter the item:75 Enter the item:42 printing the list items 2546127542
从列表中删除元素
Python 提供了 remove() 函数,用于从列表中移除元素。考虑下面的例子来理解这个概念。
示例
list = [0,1,2,3,4] print("printing original list: "); for i inlist: print(i,end=" ") list.remove(2) print("\nprinting the list after the removal of first element...") for i inlist: print(i,end=" ")
输出:
printing original list: 01234 printing the list after the removal of first element... 0134
Python 列表内置函数
Python 提供了以下内置函数,可以与列表一起使用。
len()
L1 = [1,2,3,4,5,6,7,8] print(len(L1)) 8
max()
L1 = [12,34,26,48,72] print(max(L1)) 72
min()
L1 = [12,34,26,48,72] print(min(L1)) 12
list()
str = "Johnson" s = list(str) print(type(s)) <classlist>
让我们来看看几个列表示例。
示例:1 编写程序删除列表的重复元素。
list1 = [1,2,2,3,55,98,65,65,13,29] # Declare an empty list that will store unique values list2 = [] for i in list1: if i notin list2: list2.append(i) print(list2)
输出:
[1, 2, 3, 55, 98, 65, 13, 29]
例:2 写一个程序,求列表中元素的和。
list1 = [3,4,5,9,10,12,24] sum = 0 for i in list1: sum = sum+i print("The sum is:",sum)
输出:
The sumis: 67
示例:3 编写程序,找出至少包含一个公共元素的列表。
list1 = [1,2,3,4,5,6] list2 = [7,8,9,2,10] for x in list1: for y in list2: if x == y: print("The common element is:",x)