簡體   English   中英

如何在括號中的每個單詞周圍加上引號打印此句子?

[英]how do i print this sentence in brackets and with quotation marks around each word?

sentence = input("Please enter a sentence:")

words = sentence.split()

position= [0]

myList = [] 

[myList.append(x) for x in words if x not in myList]

a =(" ".join(myList))

print (a)

這段代碼允許用戶輸入一個句子,並刪除重復的單詞。 當程序輸出句子時,它將輸出如下句子:

例如,如果我的句子是“我喜歡python”,則我的程序會在屏幕上打印“ I like python

我需要幫助的是我希望程序輸出:

['I', 'like', 'python']

有什么建議嗎

您只需要在程序中打印myList。

sentence = input("Please enter a sentence:")
words = sentence.split()
position= [0]
myList = []
[myList.append(x) for x in words if x not in myList]
print(myList)

塊引用

做這個:

def removeDuplicates(oldList):
    newList = []
    for i in oldlist:
      if i not in newlist:
        newlist.append(i)
    return newList

並像這樣使用它:

sentence = input("Please enter a sentence: ")

words = removeDuplicates(sentence.split())

快速一線。 按空格分割並設置可找到唯一值:

words = list(set(sentence.split(' ')))
print words

words不會產生您想要的結果嗎?

In [9]: sentence = input("Please Enter as sentence: ")
Please Enter as sentence: 'I like Python'

In [10]: words = sentence.split()

In [11]: words
Out[11]: ['I', 'like', 'Python']

您可以使用set命令刪除重復項

sentence = input("Please enter a sentence:")
set(sentence.split())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM