Python程式輸入逗號分隔字串
當輸入或給出文字字串時,它可能在中間包含逗號。有時,任務是將句子的所有逗號分隔部分或文字字串分隔開來。這些部分可能包含單個單詞或多個單詞。這些字串部分可以進一步作為列表項輸入,或可以進行進一步處理。類似地,也需要輸入整數形式或小數形式的數字,同時用逗號分隔。在這種情況下,將它們理解為數字非常重要。本文透過四個不同的示例演示了此過程,即給定逗號分隔的字串或句子,或數字,並透過 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 中,小數作為字串輸入,然後分隔成列表。