搜尋

天元組,集合

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,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

部署可執行的Python腳本時,如何處理特定環境的配置?部署可執行的Python腳本時,如何處理特定環境的配置?May 02, 2025 am 12:07 AM

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

您如何切成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整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

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