列表:
[ ] -->符號
-->資料收集
-->異質資料的集合(不同資料型態)
-->列表是基於索引的
-->列表是可變的(Changeable)
例如:student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6]
索引 --> 0 1 2 3 4
範例:使用 while 迴圈和 for 迴圈:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] i = 0 while i<len print i for data in student_data:> <p>輸出:<br> </p> <pre class="brush:php;toolbar:false">Guru Prasanna B.Com 23 True 5.6 Guru Prasanna B.Com 23 True 5.6
enumerate()-->對於索引追蹤很有用
Enumerate 是 python 中的內建函數,可讓您追蹤循環中的迭代(循環)次數。
語法:enumerate(iterable, start=0)
--> Iterable:任何支援迭代的物件
--> Start:計數器啟動的索引值,預設為0
範例:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] index = 0 for index,data in enumerate(student_data): print(index, data) index+=1
輸出:
0 Guru Prasanna 1 B.Com 2 23 3 True 4 5.6
證明清單是可變的
例:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] print(student_data) student_data[1] = 'M.Com' print(student_data)
輸出:
['Guru Prasanna', 'B.Com', 23, True, 5.6] ['Guru Prasanna', 'M.Com', 23, True, 5.6]
列出函數:
1)append()-->在列表末尾加上一個元素
2) insert()-->在指定位置新增元素
3)remove()-->刪除具有指定值的第一個項目(基於值的刪除)。
4) pop()-->刪除指定位置的元素(基於索引的刪除)。
參考- https://www.w3schools.com/python/python_ref_list.asp
範例:
employee = [] employee.append('Raja') employee.append('Madurai') employee.append('B.Sc') employee.append(5.2) employee.append(True) print(employee) employee.insert(2, 'Tamil Nadu') print(employee) employee.remove('Madurai') print(employee) employee.pop(3) print(employee)
輸出:
['Raja', 'Madurai', 'B.Sc', 5.2, True] ['Raja', 'Madurai', 'Tamil Nadu', 'B.Sc', 5.2, True] ['Raja', 'Tamil Nadu', 'B.Sc', 5.2, True] ['Raja', 'Tamil Nadu', 'B.Sc', True]
刪除關鍵字:
del 關鍵字用於刪除物件。 (變數、列表或列表的一部分等。)
-->連 del 也可以用來刪除特定範圍。
範例:
l = [10,20,30,40,50,60] del l[2:4] print(l)
輸出:
[10, 20, 50, 60]
del 與 pop 的差別:
del 將刪除指定的索引。 (關鍵字)
pop() 刪除並傳回被刪除的元素。 (內建方法)
計算總分和百分比
# Total, Percentage marks_list = [90,97,97,65,78] total = 0 l=len(marks_list) for mark in marks_list: total+=mark print(total) percentage=total/l print("percentage:",percentage)
輸出:
427 percentage: 85.4
計算最高分
# Highest Mark marks_list = [90,97,96,65,98] highest = marks_list[0] for mark in marks_list: if mark>highest: highest = mark print(highest)
輸出:
98
計算最低分
# lowest Mark marks_list = [90,97,96,65,98] lowest = marks_list[0] for mark in marks_list: if mark<lowest: lowest="mark" print> <p>輸出:<br> </p> <pre class="brush:php;toolbar:false">65
isinstance(): isinstance() 函數如果指定物件屬於指定類型則傳回 True,否則傳回 False。
例:1
data_list = ['abcd','pqrs','xyz',1234, 1.234,True] for data in data_list: if isinstance(data,str): print(data)
輸出:
abcd pqrs xyz
範例:2
#Find str datatype and make them to uppercase data_list = ['abcd','pqrs','xyz',1234, 1.234,True] for data in data_list: if isinstance(data,str): print(data.upper())
輸出:
ABCD PQRS XYZ
範例:3
#Find str datatype and print only first 2 letters data_list = ['abcd','pqrs','xyz','a','m',1234, 1.234,True] for data in data_list: if isinstance(data,str): if len(data)>= 2: print(data.upper()[:2])
輸出:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] i = 0 while i<len print i for data in student_data:> <p><strong>任務:</strong><br> 1) 包含n -->名字<br> 2) 名字有5個字母<br> 3) t——>名字以<br>結尾 </p> <pre class="brush:php;toolbar:false">Guru Prasanna B.Com 23 True 5.6 Guru Prasanna B.Com 23 True 5.6
輸出:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] index = 0 for index,data in enumerate(student_data): print(index, data) index+=1
4) SaChIn DhOnI rOhIt vIrAt-->取得此輸出
0 Guru Prasanna 1 B.Com 2 23 3 True 4 5.6
輸出:
student_data = ['Guru Prasanna', 'B.Com', 23, True, 5.6] print(student_data) student_data[1] = 'M.Com' print(student_data)
以上是Python Day-Lists 與列表函數,任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ArraySareAryallyMoremory-Moremory-forigationDataDatueTotheIrfixed-SizenatureAntatureAntatureAndirectMemoryAccess.1)arraysStorelelementsInAcontiguxufulock,ReducingOveringOverheadHeadefromenterSormetormetAdata.2)列表,通常

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Python列表可以存儲不同類型的數據。示例列表包含整數、字符串、浮點數、布爾值、嵌套列表和字典。列表的靈活性在數據處理和原型設計中很有價值,但需謹慎使用以確保代碼的可讀性和可維護性。

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

theSostCommonlyusedModuleForCreatingArraysInpyThonisnumpy.1)NumpyProvidEseffitedToolsForarrayOperations,Idealfornumericaldata.2)arraysCanbeCreatedDusingsnp.Array()for1dand2Structures.3)

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版
中文版,非常好用