首先我做了這個
text = "Hello I want to make this code better"
x = text.split()
sorted_characters = sorted(x[5])
sortedword = "".join(sorted_characters)
print(sortedword)
我希望代碼能夠做到這一點,但我不知道如何將單詞和串列中的每個字串分開:
example=['I like python', 'I like reading books']
輸出應該是 ['I eikl hnopty', 'I eikl adeginp bkoos']
uj5u.com熱心網友回復:
一個班輪:
[' '.join([''.join(sorted(z)) for z in i.split()]) for i in example]
這只是做你所做的,使用python的串列理解功能,并對串列中的每個元素進行排序
uj5u.com熱心網友回復:
您可以使用split從句子中取出單詞并對每個單詞進行排序并將它們添加到一個新字串中,就像這樣......
text = "Hello I want to make this code better"
res = ""
for i in text.split():
temp = ""
for j in sorted(i):
temp = j
res = f"{temp} "
print(res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/392872.html
下一篇:計算火車能否過橋
