Python Day-Tuples,集合:方法、示例、任务

wufei123 2025-01-05 阅读:11 评论:0
元组: -->元组项是有序的,不可变的(不可更改),并且允许重复值。 -->元组用圆括号()书写。 -->tuples 还允许索引、切片。 -->元组与列表类似,可以执行加法、乘法,很少有相同的功能也可以用于元组。...

元组:

-->元组项是有序的,不可变的(不可更改),并且允许重复值。
-->元组用圆括号()书写。
-->tuples 还允许索引、切片。
-->元组与列表类似,可以执行加法、乘法,很少有相同的功能也可以用于元组。

示例:

t = (10,20,30)
print('output:1',t)
print('output:2',type(t))

print('output:3',end=' ')
for num in t:
    print(num, end = ' ')

total = 0
print('output:4',end=' ')
for num in t:
    total+=num
print(total)

t[0] = 100

输出:

output:1 (10, 20, 30)
output:2 <class 'tuple'>
output:3 10 20 30 output:4 60
'tuple' object does not support item assignment

-->对于最后一个输出,它显示错误,因为元组是不可变的项目分配无法完成。

元组打包和解包:

tuple packing and unpacking are features that allow you to group values into a tuple and extract them back into individual variables.

示例:

#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

相同的函数可以用作列表函数。

示例:

t1 = 10,20,30,40,50,60,10

print(t1.count(10))
print(t1.index(20))
print(sorted(t1))

print(sorted(t1,reverse=false))

输出:

2
1
[10, 10, 20, 30, 40, 50, 60]
[10, 10, 20, 30, 40, 50, 60]

1) 找到
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(data):
    print(data[i][1],end=' ')
    i+=1

输出:

[40, 50, 60]
60,150,240,
20 50 80

eval() 函数:

此函数用于评估通过 input() 函数提供的元素类型是列表还是元组。

t = eval(input("enter tuple elements: "))
print(type(t))
print(t)

输出:

enter tuple elements: 10,20,30
<class 'tuple'>
(10, 20, 30)

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 检查内存。

--> == 运算符帮助我们比较对象的相等性。
--> 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

设置:
-->sets 用于在单个变量中存储多个项目。
-->集合是无序、不可变(不可更改)、无索引的集合。
-->忽略重复项。

设置方法:
1)union():(符号-|)返回包含集合并集的集合。

2)intersection():(symbol-&)返回一个集合,即其他两个集合的交集。

3)difference():(符号:'-')返回包含两个或多个集合之间差异的集合。

4)symmetry_difference():(symbol-^)返回具有两个集合的对称差的集合。

示例:1

s1 = {10,20,30,40}
s2 = {30,40,50,60}
print(s1.union(s2))
print(s1 | s2)

print(s1.intersection(s2))
print(s1 & s2)

print(s1.difference(s2))
print(s1 - s2)

print(s1.symmetric_difference(s2))
print(s1 ^ s2)

输出:

{40, 10, 50, 20, 60, 30}
{40, 10, 50, 20, 60, 30}
{40, 30}
{40, 30}
{10, 20}
{10, 20}
{10, 50, 20, 60}
{10, 50, 20, 60}

示例:2

s1 = {10,20}
s2 = {20,30}
s3 = {30,40}

print(s1.union(s2,s3))

result = s1 | s2 | s3
print(result)

输出:

{20, 40, 10, 30}
{20, 40, 10, 30}

注意:我们可以使用符号或方法名称。

丢弃():

--> 仅当该元素存在于集合中时才从集合中删除该元素。
--> 如果集合中不存在该元素,则不会引发错误或异常,并打印原始集合。

discard() 和 remove() 之间的区别
-->remove():仅当该元素存在于集合中时才从集合中删除该元素,就像discard()方法一样,但如果该元素不存在于集合中,则会引发错误或异常。
-->in discard() 不会引发错误或异常,并打印原始集。
参考-https://www.geeksforgeeks.org/python-remove-discard-sets/

示例:

s = {"abcd", 1.2, true, 500,500}
s.remove(10)
print(s)

s.discard(10)
print(s)

输出:

keyerror: 10
{'abcd', 1.2, 500, true}

任务:
match1 = {"sanju", "virat", "ashwin", "rohit"}

match2 = {"dhoni", "virat", "bumrah", "siraj"}
找到以下内容:

a) 匹配 1、匹配 2
b)参加了第一场比赛,但没有参加第二场比赛
c)参加了第 2 场比赛,但未参加第 1 场比赛
d)只参加了一场比赛

match1 = {"sanju", "virat", "ashwin", "rohit"}

match2 = {"dhoni", "virat", "bumrah", "siraj"}
#a
print(match1 & match2)
#b
print(match1 - match2)
#c
print(match2 - match1)
#d
print(match1 ^ match2)

输出:

{'virat'}
{'sanju', 'rohit', 'ashwin'}
{'dhoni', 'siraj', 'bumrah'}
{'sanju', 'bumrah', 'rohit', 'siraj', 'ashwin', 'dhoni'}

以上就是Python Day-Tuples,集合:方法、示例、任务的详细内容,更多请关注知识资源分享宝库其它相关文章!

版权声明

本站内容来源于互联网搬运,
仅限用于小范围内传播学习,请在下载后24小时内删除,
如果有侵权内容、不妥之处,请第一时间联系我们删除。敬请谅解!
E-mail:dpw1001@163.com

分享:

扫一扫在手机阅读、分享本文

发表评论
热门文章
  • 华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀

    华为 Mate 70 性能重回第一梯队 iPhone 16 最后一块遮羞布被掀
    华为 mate 70 或将首发麒麟新款处理器,并将此前有博主爆料其性能跑分将突破110万,这意味着 mate 70 性能将重新夺回第一梯队。也因此,苹果 iphone 16 唯一能有一战之力的性能,也要被 mate 70 拉近不少了。 据悉,华为 Mate 70 性能会大幅提升,并且销量相比 Mate 60 预计增长40% - 50%,且备货充足。如果 iPhone 16 发售日期与 Mate 70 重合,销量很可能被瞬间抢购。 不过,iPhone 16 还有一个阵地暂时难...
  • 酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元

    酷凛 ID-COOLING 推出霜界 240/360 一体水冷散热器,239/279 元
    本站 5 月 16 日消息,酷凛 id-cooling 近日推出霜界 240/360 一体式水冷散热器,采用黑色无光低调设计,分别定价 239/279 元。 本站整理霜界 240/360 散热器规格如下: 酷凛宣称这两款水冷散热器搭载“自研新 V7 水泵”,采用三相六极马达和改进的铜底方案,缩短了水流路径,相较上代水泵进一步提升解热能力。 霜界 240/360 散热器的水泵为定速 2800 RPM 设计,噪声 28db (A)。 两款一体式水冷散热器采用 27mm 厚冷排,...
  • 惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起

    惠普新款战 99 笔记本 5 月 20 日开售:酷睿 Ultra / 锐龙 8040,4999 元起
    本站 5 月 14 日消息,继上线官网后,新款惠普战 99 商用笔记本现已上架,搭载酷睿 ultra / 锐龙 8040处理器,最高可选英伟达rtx 3000 ada 独立显卡,售价 4999 元起。 战 99 锐龙版 R7-8845HS / 16GB / 1TB:4999 元 R7-8845HS / 32GB / 1TB:5299 元 R7-8845HS / RTX 4050 / 32GB / 1TB:7299 元 R7 Pro-8845HS / RTX 2000 Ada...
  • python中def什么意思

    python中def什么意思
    python 中,def 关键字用于定义函数,这些函数是代码块,执行特定任务。函数语法为 def (参数列表)。函数可以通过其名字和圆括号调用。函数可以接受参数作为输入,并在函数体中使用参数名访问。函数可以使用 return 语句返回一个值,它将成为函数调用的结果。 Python 中 def 关键字 在 Python 中,def 关键字用于定义函数。函数是代码块,旨在执行特定任务。 语法 def 函数定义的语法如下: def (参数列表): # 函数体 示例 定义...
  • python中int函数的用法

    python中int函数的用法
    int() 函数将值转换为整数,支持多种类型(字符串、字节、浮点数),默认进制为 10。可以指定进制数范围在 2-36。int() 返回 int 类型的转换结果,丢弃小数点。例如,将字符串 "42" 转换为整数为 42,将浮点数 3.14 转换为整数为 3。 Python 中的 int() 函数 int() 函数用于将各种类型的值转换为整数。它接受任何可以解释为整数的值作为输入,包括字符串、字节、浮点数和十六进制表示。 用法 int(object, base=10) 其中...