我試圖創建一個集合,它將包含用戶從字符串中查詢的所有單詞。在Python中檢查截斷的文本
# Step 1: Read the string
instr = 'Lorem ipsum dolor sit amet, unde curabitur nonummy nonummy, cras dui eget, accumsan placerat nibh cumque vel augue, vitae laoreet at. Urna feugiat. Ut ante neque duis neque metus lectus. Neque rutrum tortor adipiscing omnis neque urna, diam vivamus. Pulvinar quis id, porta class felis enim vulputate, tempus arcu, sit porta. Nec praesent orci in, orci id duis purus ultrices, non sit vel. Cras ipsum nibh nulla suspendisse neque, mi dis, vestibulum a ipsum, morbi a curabitur porttitor sodales sit nullam, mus tristique feugiat vel per orci. Lacus elit molestie quam, ac nibh nulla in faucibus quis volutpat. Congue cum massa. Convallis in ultricies eget velit, erat elit eleifend.'
text_lowercase = instr.lower()
# Turn them in to set. Easy enough
all_words = set(text_lowercase)
# Step 2: Compare with the user's words:
all_words & user_words
# And those are the user's words that exist in the text.
我的問題是
假設爲條件的用戶查詢,如lor*
,或con*
。 我希望能夠添加到我的set
每一個字,開始與lor
或con
。從一個快速通過,我看到有像Congue
,Convallis
這樣的詞。所以這2個應該被添加到集合中。
您是否嘗試過打印'all_words'?我不認爲它在做你期望的。 – asongtoruin
你到目前爲止嘗試過什麼?你應該發佈更多的代碼來展示你的努力,否則它看起來像你希望社區做你的功課。看看python字符串函數'split'和'startswith'。 – Risadinha
是的,你可能需要拆分該字符串,然後將其更改爲一組來完成你的目標。但總的來說,你的問題的答案是學習如何使用[regex庫](https://docs.python.org/3/library/re.html) – Brionius