from collections import OrderedDict
sentence= ("I met a traveller from an antique land,Who said—「Two vast and trunkless legs of stoneStand in the desert. . . . Near them, on the sand,Half sunk a shattered visage lies, whose frown,And wrinkled lip, and sneer of cold command,Tell that its sculptor well those passions readWhich yet survive, stamped on these lifeless things,The hand that mocked them, and the heart that fed;And on the pedestal, these words appear:My name is Ozymandias, King of Kings;Look on my Works, ye Mighty, and despair!Nothing beside remains. Round the decayOf that colossal Wreck, boundless and bareThe lone and level sands stretch far away.").lower()
字= sentence.split(」「)創建從一個列表中的文件用空格
lst = list(OrderedDict.fromkeys(words))
numberLst = []
for i in words:
numberLst.append(lst.index(i)+1)
print(lst)
print (numberLst)
words_str = ''.join(words)
numberLst_str = ''.join(str(e) for e in numberLst)
file = open("words.txt","w")
file.write(words_str)
file.close()
file=open("numberlst.txt","w")
file.write(numberLst_str)
file.close()
joinlst = " ".join(lst[i-1] for i in numberLst)
print (joinlst)
file=open("joinlst.txt","w")
file.write(joinlst)
file.close()
您好,我有一個問題,我的話的文件,我在我的代碼已經創建,它是當我連接我列表中的所有單詞時,它會被寫入文件中:imetatravellerfromananlandland,whosaid-「兩個無邊無角的石塊,擱置在插入的......黑暗,背景,半暗視野,視角,告訴它, andonthepedestal,thesewordsappear:mynameisozymandia S,kingofkings;!lookonmyworks,yemighty,anddespair nothingbesideremains.roundthedecayofthatcolossalwreck,boundlessandbaretheloneandlevelsandsstretchfaraway。
問題是在文件中寫入的單詞之間沒有空格,因此很難識別文件中的單詞。一旦修好它最好看起來像這樣:(我:遇見:a:旅客:從:an:古董:土地)(僅作爲例子)
如果你能請幫助它將不勝感激,謝謝。
我不是在追隨你想要的?你想只寫出用':'分開的單詞嗎? –
您好像在'''.join(words)'中加入了一個空字符串的單詞。在那裏添加冒號,所以'':'。join(words)'。但是,你稍後在'joinlst =「」.join(lst [i-1]爲我在numberLst中)正確地使用一個空格'',所以我不確定我是否理解。 – roganjosh