搜索
首页后端开发Python教程Python Day-Lists 和列表函数,任务

Python Day-Lists and list functions,Task

列表:
[ ] -->符号
-->数据收集
-->异构数据的集合(不同数据类型)
-->列表是基于索引的
-->列表是可变的(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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
为什么数组通常比存储数值数据列表更高?为什么数组通常比存储数值数据列表更高?May 05, 2025 am 12:15 AM

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

如何将Python列表转换为Python阵列?如何将Python列表转换为Python阵列?May 05, 2025 am 12:10 AM

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

您可以将不同的数据类型存储在同一Python列表中吗?举一个例子。您可以将不同的数据类型存储在同一Python列表中吗?举一个例子。May 05, 2025 am 12:10 AM

Python列表可以存储不同类型的数据。示例列表包含整数、字符串、浮点数、布尔值、嵌套列表和字典。列表的灵活性在数据处理和原型设计中很有价值,但需谨慎使用以确保代码的可读性和可维护性。

Python中的数组和列表之间有什么区别?Python中的数组和列表之间有什么区别?May 05, 2025 am 12:06 AM

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

通常使用哪种模块在Python中创建数组?通常使用哪种模块在Python中创建数组?May 05, 2025 am 12:02 AM

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

您如何将元素附加到Python列表中?您如何将元素附加到Python列表中?May 04, 2025 am 12:17 AM

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

您如何创建Python列表?举一个例子。您如何创建Python列表?举一个例子。May 04, 2025 am 12:16 AM

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

讨论有效存储和数值数据的处理至关重要的实际用例。讨论有效存储和数值数据的处理至关重要的实际用例。May 04, 2025 am 12:11 AM

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

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器