在Python中按字典順序對單詞進行排序

| | | |

例子:

輸入:“hello python program how are you” 輸出:are hello how program python you 輸入:“Coders love the algorithms” 輸出:Coders algorithms love the 

注意:第一個字母的單詞是大寫字母,會按字母順序打印。

匹配:
這個程序中使用的方法很簡單。使用 split () 函數拆分行。之後,使用 sort() 按字典順序對單詞進行排序。循環重複單詞並打印每個已排序的單詞。


# 用於按字典順序排序單詞的 Python 程序
# order


def sortLexo (my_string) :


# 分割字符串 my_string 直到找到空間。

words = my_string.split ()

# sort () 將對行進行排序。

words.sort ()


# 我遍歷 "words" 以打印單詞

# 按字母順序。

for i in words:

print (i)


# 驅動代碼

if __ name__ = = ` __main__` :


my_string = " 你好這是如何排序的例子 "

"按字母順序排列的單詞"

# 函數調用

sortLexo(my_string)

歐tput:

字母示例你好,如何將 this 排序為單詞

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method