當輸入文字字串或作為輸入給出時,其間可能有逗號。有時,任務是分隔句子或文字字串的所有逗號分隔部分。這些部分可以具有單字或多個單字。這些字串部分可以作為列表項目進一步輸入或可以進一步處理。同樣,也需要輸入整數形式或小數形式的數字,並用逗號分隔。在這種情況下,將它們理解為數字很重要。本文透過使用四個不同的範例,演示了給定逗號分隔的字串或句子或數字,並透過 Python 程式理解其逗號分隔結構來處理它的過程。
範例 1 - 輸入逗號分隔字串並使用 split 函數尋找逗號分隔部分的程式
演算法
第 1 步 - 首先輸入一個以逗號分隔的字串。
步驟 2 - 使用 split 函數將逗號分隔的部分分隔成清單。
第 3 步 - 刪除清單項目左側的空格。
第 4 步 - 刪除清單項目右側的空格。
第 5 步 - 執行程序,然後檢查結果。
Python 檔案包含此
commaSepStr = input ("Enter a comma separated String:") list1 = commaSepStr.split(",") def removeLspace(list): return [item.lstrip() for item in list] print(commaSepStr) print(list1) def removeRspace(list): return [item.rstrip() for item in list] noextraleftspace_list = removeLspace(list1) noextrarightspace_list = removeRspace(noextraleftspace_list) print(noextrarightspace_list) print(*noextrarightspace_list, sep = "\n")
檢視結果 - 範例 1
要查看結果,請在 cmd 視窗中執行 Python 檔案。
Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar ['Our last night plate included two rotis', 'daal', 'mixveg', ' rice', ' paneer', ' salad and achaar'] ['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar'] Our last night plate included two rotis daal mixveg rice paneer salad and achaar
範例 2:輸入逗號分隔字串並使用「for」迴圈尋找逗號分隔部分的程式。
演算法
第 1 步 - 首先給出一個以逗號分隔的輸入字串。
第 2 步 - 逐個字元地遍歷字串並識別逗號分隔的部分並將它們附加到清單中。
第 3 步 - 刪除清單項目左側的空格。
第 4 步 - 列印包含沒有多餘空格的項目的清單。
第 5 步 - 執行程序,然後檢查結果。
Python 檔案包含此
commaSepStr = input ("Enter a comma separated String :") print("The Entered String is: " + commaSepStr) startofItem = 0 list1=[] for item in range(len(commaSepStr)): if commaSepStr[item] == ',': # characters from startofItem to comma nospaceitem=commaSepStr[startofItem:item].lstrip() list1.append(nospaceitem) startofItem = item+1 print(nospaceitem) # characters from startofItem to end nospaceitem=commaSepStr[startofItem:].lstrip() print(nospaceitem) list1.append(nospaceitem) print(list1))
查看結果
開啟cmd視窗並執行python檔案查看結果。
Enter a comma separated String :Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar The Entered String is: Our last night plate included two rotis,daal,mixveg, rice, paneer, salad and achaar Our last night plate included two rotis daal mixveg rice paneer salad and achaar ['Our last night plate included two rotis', 'daal', 'mixveg', 'rice', 'paneer', 'salad and achaar']
範例 3 - 輸入逗號分隔的整數字串的程式
演算法
步驟 1 - 首先輸入一個以逗號分隔且僅包含整數的字串。
第 2 步 - 使用 split 函數將逗號分隔的整數分隔成字串清單。
第 3 步 - 從此字串清單中取得每個項目,並將它們轉換為整數類型,並將它們作為整數附加到另一個清單。
第 4 步 - 執行程序,然後檢查結果。
Python 檔案包含此
# input comma-separated numbers as string strInput = input ("Enter comma separated integers: ") print( "Input string: ", strInput) # convert to the list strlist = strInput.split(",") print("list of string type numbers: ", strlist) # convert each string element as integers list1 = [] for item in strlist: list1.append(int(item)) # print list as integers print("list of integers: ", list1)
檢視結果 - 範例 3
要查看結果,請在 cmd 視窗中執行 Python 檔案。
Enter comma separated integers: 101, 280, 98, 185, 934, 9684, 955, 20, 34 Input string: 101, 280, 98, 185, 934, 9684, 955, 20, 34 list of string type numbers: ['101', ' 280', ' 98', ' 185', ' 934', ' 9684', ' 955', ' 20', ' 34'] list of integers: [101, 280, 98, 185, 934, 9684, 955, 20, 34]
範例 4:輸入具有十進位數字的逗號分隔字串的程式
第 1 步 - 首先輸入一個以逗號分隔的字串,並且只包含整數和小數。
步驟 2 - 使用 split 函數來識別逗號分隔的數字並將它們作為字串附加到清單中。
第 3 步 - 從此字串清單中取出每個數字,並將它們轉換為浮點類型,並將它們作為小數附加到另一個清單。
第 4 步 - 執行程序,然後檢查結果。
Python 檔案包含此
# input comma separated numbers as string strInput = input ("Enter comma separated numbers: ") print( "Input string: ", strInput) # convert to the list strlist = strInput.split (",") print("list of string type numbers: ", strlist) # convert each string element as integers list1 = [] for item in strlist: list1.append(float(item)) # print list as integers print("list of decimal numbers: ", list1)
檢視結果 - 範例 4
開啟cmd視窗並執行python檔案查看結果。
Enter comma-separated numbers: 102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009 Input string: 102.88, 6.5, 6767.907, 5555.3, 4545, 6677,56.009 list of string type numbers: ['102.88', ' 6.5', ' 6767.907', ' 5555.3', ' 4545', ' 6677', '56.009'] list of decimal numbers: [102.88, 6.5, 6767.907, 5555.3, 4545.0, 6677.0, 56.009]
圖 4:顯示具有十進制數字的輸入字串中以逗號分隔的部分的清單。
在這篇 Python 文章中,使用四個不同的範例,給出如何輸入逗號分隔字串的方法。首先,在範例 1 中,使用 split 函數將字串的各個部分以逗號分隔。在範例 2 中,透過檢查所有字元來遍歷字串來識別逗號分隔的部分。在範例 3 中,整數作為字串輸入,在範例 4 中,十進制數作為輸入字串給出,然後分隔到列表中。
以上是Python程式:輸入逗號分隔的字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。

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

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

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

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

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


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具

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

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)