Here, we are implementing a python program for various list operations, following operations are being performed in the list,
在这里,我们正在为各种列表操作实现python程序,正在列表中执行以下操作,
Declaring an integer list
声明一个整数列表
Printing the complete list
打印完整列表
Printing the first element of the list
打印列表的第一个元素
Printing ith element of the list
打印列表的第i个元素
Printing elements within the given indexes
在给定索引中打印元素
Printing a specific element using negative indexing
使用负索引打印特定元素
Appending an element to the list
将元素追加到列表
Finding the index of a specific element in the list
在列表中查找特定元素的索引
Sorting the list elements
排序列表元素
Popping an element from the list
从列表中弹出一个元素
Removing specified element from the list
从列表中删除指定的元素
Inserting an element at specified index
在指定索引处插入元素
Extending the list i.e. insert set of element (list) in the list
扩展列表,即在列表中插入一组元素(列表)
Reversing list elements
反转列表元素
用于各种列表操作的Python代码 (Python code for various list operation)
# Python code for various list operation
# declaring a list of integers
iList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# List slicing operations
# printing the complete list
print('iList: ',iList)
# printing first element
print('first element: ',iList[0])
# printing fourth element
print('fourth element: ',iList[3])
# printing list elements from 0th index to 4th index
print('iList elements from 0 to 4 index:',iList[0: 5])
# printing list -7th or 3rd element from the list
print('3rd or -7th element:',iList[-7])
# appending an element to the list
iList.append(111)
print('iList after append():',iList)
# finding index of a specified element
print('index of \'80\': ',iList.index(80))
# sorting the elements of iLIst
iList.sort()
print('after sorting: ', iList);
# popping an element
print('Popped elements is: ',iList.pop())
print('after pop(): ', iList);
# removing specified element
iList.remove(80)
print('after removing \'80\': ',iList)
# inserting an element at specified index
# inserting 100 at 2nd index
iList.insert(2, 100)
print('after insert: ', iList)
# counting occurances of a specified element
print('number of occurences of \'100\': ', iList.count(100))
# extending elements i.e. inserting a list to the list
iList.extend([11, 22, 33])
print('after extending:', iList)
#reversing the list
iList.reverse()
print('after reversing:', iList)
Output
输出量
iList: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
first element: 10
fourth element: 40
iList elements from 0 to 4 index: [10, 20, 30, 40, 50]
3rd or -7th element: 40
iList after append(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]
index of '80': 7
after sorting: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 111]
Popped elements is: 111
after pop(): [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
after removing '80': [10, 20, 30, 40, 50, 60, 70, 90, 100]
after insert: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100]
number of occurences of '100': 2
after extending: [10, 20, 100, 30, 40, 50, 60, 70, 90, 100, 11, 22, 33]
after reversing: [33, 22, 11, 100, 90, 70, 60, 50, 40, 30, 100, 20, 10]
翻译自: https://www.includehelp.com/python/program-for-various-list-operations.aspx