搜索

天元组,集合

Jan 03, 2025 pm 06:17 PM

Day-Tuples, Set

元组:
元组维持元素定义的顺序。
元组一旦创建,其内容就无法更改。
与列表一样,元组可以包含重复的值。
元组可以存储混合类型的数据,包括其他元组、列表、整数、字符串等
您可以通过索引访问元组元素,从 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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

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

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

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

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

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

列表的内存足迹与python数组的内存足迹相比如何?列表的内存足迹与python数组的内存足迹相比如何?May 02, 2025 am 12:08 AM

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

部署可执行的Python脚本时,如何处理特定环境的配置?部署可执行的Python脚本时,如何处理特定环境的配置?May 02, 2025 am 12:07 AM

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

您如何切成python阵列?您如何切成python阵列?May 01, 2025 am 12:18 AM

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

在什么情况下,列表的表现比数组表现更好?在什么情况下,列表的表现比数组表现更好?May 01, 2025 am 12:06 AM

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

如何将Python数组转换为Python列表?如何将Python数组转换为Python列表?May 01, 2025 am 12:05 AM

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

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

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

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

螳螂BT

螳螂BT

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

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

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