元组:
元组维持元素定义的顺序。
元组一旦创建,其内容就无法更改。
与列表一样,元组可以包含重复的值。
元组可以存储混合类型的数据,包括其他元组、列表、整数、字符串等
您可以通过索引访问元组元素,从 0 开始。
由 () 表示的元组。
t = (10,20,30) print(t) print(type(t)) for num in t: print(num) total = 0 for num in t: total+=num print(total) t[0] = 100
(10, 20, 30) <class> 10 20 30 60 TypeError: 'tuple' object does not support item assignment </class>
元组包装:
通过将多个元素分组在一起来创建元组,例如 my_tuple = (1, 2, 3).
元组拆包:
将元组的元素提取到各个变量中,例如,a, b, c = my_tuple。
#Tuple Packing t = 10,20,30 print(t) #Tuple Unpacking no1, no2, no3 = t print(no1) print(no2) print(no3)
(10, 20, 30) 10 20 30
t = 10,20,30,40,50,60 print(t[:2])
(10, 20)
t1 = 10,20,30 t2 = 40,50,60 print(t1+t2) print(t1*3) print(10 in t1) print(10 not in t1)
(10, 20, 30, 40, 50, 60) (10, 20, 30, 10, 20, 30, 10, 20, 30) True False
t1 = 10,20,30,40,50,60,10 print(t1.count(10)) print(t1.index(20)) print(sorted(t1)) print(sorted(t1,reverse=True))
2 1 [10, 10, 20, 30, 40, 50, 60] [60, 50, 40, 30, 20, 10, 10]
t = ((10,20,30), (40,50,60)) print(t) print(t[0]) print(t[1]) print(t[0][0]) print(t[1][2]) t = ([10,20,30],[40,50,60]) print(t[0]) print(t[0][2])
((10, 20, 30), (40, 50, 60)) (10, 20, 30) (40, 50, 60) 10 60 [10, 20, 30] 30
编写一个程序来查找
a)第二个列表
b) 列出总计
c) 仅打印每个列表中的第二个元素。
数据 = ([10,20,30],[40,50,60],[70,80,90])
data = ([10,20,30],[40,50,60],[70,80,90]) #Second List print(data[1]) #List wise total for inner in data: total = 0 for num,index in enumerate(inner): total+=index print(total,end=' ') #Print Only second element from each list. print() i=0 while i<len print i> <pre class="brush:php;toolbar:false">[40, 50, 60] 60,150,240, 20 50 80
eval():
eval() 是一个内置的 Python 函数,用于将字符串计算为 Python 表达式并返回结果。
没有元组理解。
t = eval(input("Enter tuple Elements: ")) print(type(t)) print(t)
Enter tuple Elements: 10,20,30 <class> (10, 20, 30) </class>
next() 函数:
next() 函数返回迭代器中的下一个项目。
t = (no for no in range(1,11)) print(next(t)) print(next(t)) print(next(t)) print(next(t))
1 2 3 4
*“is”和“==”之间的区别:*
“==”被称为相等运算符。
“is”被称为恒等运算符。
== 检查值。
是检查内存。
== 运算符帮助我们比较对象的相等性。
is 运算符帮助我们检查不同的变量是否指向内存中的相似对象。
示例:
列表:
l1 = [10,20,30] l2 = l1 print(id(l1)) print(id(l2)) print(l1 == l2) print(l1 is l2) l2 = list(l1) print(id(l2)) print(l1 == l2) print(l1 is l2)
124653538036544 124653538036544 True True 124653536481408 True False
对于元组:
l1 = (10,20,30) l2 = l1 print(id(l1)) print(id(l2)) print(l1 == l2) print(l1 is l2) l2 = tuple(l1) print(id(l2)) print(l1 == l2) print(l1 is l2)
130906053714624 130906053714624 True True 130906053714624 True True
元组与列表:
元组是不可变对象,列表是可变对象。
元组使用的内存更少,并且访问速度比列表更快。
由于元组是不可变的,因此大小将小于列表。
示例:
import sys l = [10,20,30,40] t = (10,20,30,40) print(sys.getsizeof(l)) print(sys.getsizeof(t))
88 72
设置:
集合用于在单个变量中存储多个项目。
集合是无序、不可变(不可更改)且无索引的集合。
它忽略重复项。
设置方法:
1)union():(|)返回包含集合并集的集合。
2)intersection():(&)返回一个集合,即其他两个集合的交集。
3)difference():(-)返回包含两个或多个集合之间差异的集合。
4)symmetry_difference():(^)返回两个集合的对称差异的集合。
示例:1
t = (10,20,30) print(t) print(type(t)) for num in t: print(num) total = 0 for num in t: total+=num print(total) t[0] = 100
(10, 20, 30) <class> 10 20 30 60 TypeError: 'tuple' object does not support item assignment </class>
示例:2
#Tuple Packing t = 10,20,30 print(t) #Tuple Unpacking no1, no2, no3 = t print(no1) print(no2) print(no3)
(10, 20, 30) 10 20 30
丢弃():
Discard() 方法从集合中删除元素(如果存在)。如果该元素不存在,则不会执行任何操作(不会引发错误)。
删除():
如果元素存在,remove() 方法会从集合中删除该元素。如果该元素不存在,则会引发 KeyError。
t = 10,20,30,40,50,60 print(t[:2])
(10, 20)
任务:
match1 = {"sanju", "virat", "ashwin", "rohit"}
match2 = {"dhoni", "virat", "bumrah", "siraj"}
找到以下内容:
a) 匹配 1、匹配 2
b)参加了第一场比赛,但没有参加第二场比赛
c)参加了第 2 场比赛,但未参加第 1 场比赛
d)只参加了一场比赛
t1 = 10,20,30 t2 = 40,50,60 print(t1+t2) print(t1*3) print(10 in t1) print(10 not in t1)
(10, 20, 30, 40, 50, 60) (10, 20, 30, 10, 20, 30, 10, 20, 30) True False
以上是天元组,集合的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。