搜尋
首頁後端開發Python教學Python 日期列表函數、任務

Python Day-List functions, Tasks

split():將字串轉換為列表,並以逗號分隔。
join():它將列表轉換為字串。
例:

1。反轉給定的輸入-->星期四是今天

方法:1 使用循環

s = "today is thursday"
reverse = ""
i = 0

while i<len reverse="reverse" s i print si yadot word="" while if continue else:>



<p>方法:2 使用清單<br>
</p>

<pre class="brush:php;toolbar:false">sen = "today is thursday"
l =  sen.split(" ")
print(l)
print(l[::-1])
s = " ".join(l[::-1])
print(s)

輸出:

yadsruht si yadot
thursday is today

深拷貝:

-->在 Python 中,指派給可變物件(如列表)的變數會保存對物件的參考。
-->如果兩個變數引用同一個對象,則透過一個變數所做的變更將反映在另一個變數中。

範例:

l1 = [10,20,30]
l2 = l1
print(l1)
print(id(l1))
print(l2)
print(id(l2))
l2[0] = 111
print(l1)
print(l2)

輸出:

[10, 20, 30]
127285488814912
[10, 20, 30]
127285488814912
[111, 20, 30]
[111, 20, 30]

淺複製:
--> copy() 方法建立一個新的列表對象,但僅複製元素的參考(對於嵌套對象)。
-->它將儲存在單獨的記憶體位址中。

範例:

l1 = [10,20,30]
l2 = l1.copy()
print(l1)
print(id(l1))
print(l2)
print(id(l2))
l2[0] = 111
print(l1)
print(l2)

輸出:

[10, 20, 30]
140500496468800
[10, 20, 30]
140500496470528
[10, 20, 30]
[111, 20, 30]

字典順序:

-->字典順序是指根據字母的字母順序按字典順序排列它們。

-->按 ASCII 順序比較。 (A-Z:65-91),(a-z:97-122)

使用比較運算子:

l1 = ['lakshmi', 'guru', 'kuhan']
l2 = ['lakshmi', 'guru', 'kuhan']
print(l1 == l2)
print(l1 != l2)

l2 = ['guru', 'lakshmi', 'kuhan']
print(l1>l2) 
print(l1<l2>



<p>輸出:<br>
</p>

<pre class="brush:php;toolbar:false">True
False
True
False

練習:面試問題

  1. 取得輸出: 10 5 10 6 10 7 20 5 20 6 20 7 30 5 30 6 30 7
l1 = [10,20,30]
l2 = [5,6,7]

for no in l1:
    for num in l2:
        print(no, num, end=' ')
    print()

輸出:

10 5 10 6 10 7 
20 5 20 6 20 7 
30 5 30 6 30 7 
  1. 如果輸出附近有 20 20,則刪除:
l1 = [10,20,30]
l2 = [8,20,7]

for no in l1:
    for num in l2:
        if no != num:
            print(no, num,end=' ')
    print()

輸出:

10 8 10 20 10 7 
20 8 20 7 
30 8 30 20 30 7 

任務:
1.從清單中找出總和為:5
的對 l1 = [1,2,3,4,5,6,7,8,9,0]

方法:1

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

pairs = []
for i in l: 
    for j in l:
        if i + j == 5:
            pairs.append((i,j))
print(pairs)

輸出:

[(1, 4), (2, 3), (3, 2), (4, 1), (5, 0), (0, 5)]

方法:2

l = [1,2,3,4,5,6,7,8,9,0]
for i in range(len(l)):
    for j in range(len(l)):
        sum = l[i] + l[j]
        if sum == 5:
            print(l[i], l[j])

輸出:

1 4
2 3
3 2
4 1
5 0
0 5

2。找出缺少的號碼
l = [10,20,30,50,60,70,80,90]

方法:1

l = [10, 20, 30, 50, 60, 70, 80, 90]
results = []

for i in range(len(l) - 1):
    if l[i + 1] - l[i] != 10:
        results.append(int((l[i] + l[i + 1]) / 2))

for result in results:
    print(result)

方法:2

l = [10,20,30,50,60,70,80,90]
i=0
while i<len if l result="(l[i]+l[i+1])/2" print i>



<p>輸出:</p>

<p>40</p>


          

            
        </len>

以上是Python 日期列表函數、任務的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何將元素附加到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,可以显著提升系统性能和可扩展性。

您如何創建Python數組?舉一個例子。您如何創建Python數組?舉一個例子。May 04, 2025 am 12:10 AM

pythonarraysarecreatedusiseThearrayModule,notbuilt-Inlikelists.1)importThearrayModule.2)指定tefifythetypecode,例如,'i'forineizewithvalues.arreaysofferbettermemoremorefferbettermemoryfforhomogeNogeNogeNogeNogeNogeNogeNATATABUTESFELLESSFRESSIFERSTEMIFICETISTHANANLISTS。

使用Shebang系列指定Python解釋器有哪些替代方法?使用Shebang系列指定Python解釋器有哪些替代方法?May 04, 2025 am 12:07 AM

除了shebang線,還有多種方法可以指定Python解釋器:1.直接使用命令行中的python命令;2.使用批處理文件或shell腳本;3.使用構建工具如Make或CMake;4.使用任務運行器如Invoke。每個方法都有其優缺點,選擇適合項目需求的方法很重要。

列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MantisBT

MantisBT

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

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。