如何使用 Python 對單詞進行字母順序排序?


在這篇文章中,我們將向您展示如何在Python中按字母順序對單詞進行排序。以下是按字母順序對單詞進行排序的方法

  • 使用氣泡排序

  • 使用sorted()函式

  • 使用sort()函式

使用氣泡排序

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立變數以儲存輸入字串。

  • 使用split() 函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格)將輸入字串拆分為單詞列表,並建立一個變數來儲存它。

  • 使用for 迴圈遍歷單詞列表中的每個單詞,直到使用len() 函式(len() 方法返回物件中的專案數。當物件是字串時,它返回字串中的字元數)遍歷單詞列表的長度。

  • 使用lower() 函式,將所有單詞轉換為小寫。

  • 應用氣泡排序對單詞進行排序並列印它們。

示例

以下程式使用 Python 中的氣泡排序返回句子的排序單詞:

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" print("Input String =",inputString) # splitting the input string into a list of words wordsList = inputString.split(" ") # traversing through each word in the words list till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # traversing through each word from the end in the words list # Applying bubble sort to sort the words for n in range(len(wordsList)-1, 0, -1): for i in range(n): if wordsList[i] > wordsList[i + 1]: # swapping data if the element is less than the next element in the array wordsList[i], wordsList[i + 1] = wordsList[i + 1], wordsList[i] # printing the list of sorted Words in alphabetic Order print("Sorted Words Alphabetically =",wordsList)

輸出

執行上述程式將生成以下輸出:

Input String = the Quick brown fox jumPs over the lazY Dog
Sorted Words Alphabetically = ['brown', 'dog', 'fox', 'jumps', 'lazy', 'over', 'quick', 'the', 'the']


使用 sorted() 函式

sorted() 函式返回給定可迭代物件的排序列表。

您可以選擇升序或降序。數字按數字排序,而字串字母順序排列。

語法

sorted(iterable, key=key, reverse=reverse)

引數

iterable- It is a sequence.
key - A function that will be executed to determine the order. The default value is None.
reverse - A Boolean expression. True sorts ascending, False sorts descending. The default value is False.

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立變數以儲存輸入字串。

  • 使用sorted()函式透過將單詞列表作為引數傳遞給它,按字母順序對單詞列表的單詞進行排序。

  • 列印按字母順序排序的單詞,並使用join() 函式(它是 Python 中的字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以形成字串)將它們與' '(空字串)連線起來以將其轉換為字串。

示例

以下程式使用 Python 中的sorted()函式返回句子的排序單詞:

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" # splitting the input string into a list of words based on spaces wordsList = inputString.split(" ") # traversing through each word till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # sorting the words of words list in alphabetical order sortedWords = sorted(wordsList) # printing the sorted Words in alphabetic Order as a string print(' '.join(sortedWords))

輸出

執行上述程式將生成以下輸出:

brown dog fox jumps lazy over quick the the

使用 sort() 函式

sort() 方法對原始列表進行就地排序。這意味著 sort() 方法會更改列表元素的順序。

簡單來說,sorts 方法按升序或降序對列表進行排序。如果它是字串,則按字母順序排序。

預設情況下,sort() 方法使用小於運算子 (<) 對列表的條目進行排序,即升序。換句話說,它優先考慮較小的元素而不是較大的元素。

要按從高到低(降序)對元素進行排序,請在sort()方法中使用 reverse=True 引數。

list.sort(reverse=True)

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立變數以儲存輸入字串。

  • 對單詞列表應用sort()函式,按字母順序對單詞列表的單詞進行排序。

  • 列印按字母順序排序的單詞,並使用join()函式將它們與' '(空字串)連線起來以將其轉換為字串。

示例

以下程式使用 Python 中的sort()函式返回句子的排序單詞:

# input string inputString = "the Quick brown fox jumPs over the lazY Dog" # splitting the input string into a list of words based on spaces wordsList = inputString.split(" ") # traversing through each word till the length of the words list for w in range(len(wordsList)): # converting all the words into lowercase using the lower() function wordsList[w]=wordsList[w].lower() # sorting the words of words list in alphabetical order using sort() wordsList.sort() # printing the sorted Words in alphabetic Order as a string print(' '.join(wordsList))

輸出

執行上述程式將生成以下輸出:

brown dog fox jumps lazy over quick the the

結論

在本教程中,我們學習瞭如何使用三種不同的方法在 Python 中對句子中的單詞進行排序。我們還學習瞭如何使用氣泡排序對列表進行排序。

更新於: 2023年9月9日

9K+ 次檢視

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告