适用于各种列表操作的Python程序
Here, we are implementing a python program for various list operations, following operations are being performed in the list,
在這里,我們正在為各種列表操作實現(xiàn)python程序,正在列表中執(zhí)行以下操作,
Declaring an integer list
聲明一個整數(shù)列表
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
反轉(zhuǎn)列表元素
用于各種列表操作的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
總結(jié)
以上是生活随笔為你收集整理的适用于各种列表操作的Python程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css 中文文字字体_使用CSS的网络字
- 下一篇: c语言 div ldiv_C ++中带有