2015-04-04 49 views
-2

我有一個文本文件,其中包含從a到z列出的千字。它看起來例如是這樣的:Python 3:從文本文件構建一個字典,然後在字典中搜索單詞

a 
aaoo 
aloor 
azur 
black 
blue 
church 
croccoli 
dark 
den 
... 
zic 
zip 

我需要建立我的字典的鍵是小寫字母,並且其值設置包含 給定的字母詞的。例如:

myDict={'a':['aaoo','aloor','azur'], 'b':['black','blue'], 'c': ['church', 'croccoli'],'d':['dark','den'], and so on} 

然後我需要提示用戶輸入一個字和打印包含單詞的所有字符的文件中的所有字。

wordFind=("Enter word: ") 
wordFind=wordFind.lower() 
wordFind=set(wordFind) #I convert to set to use intersection 

例如,我在wordFind鍵入單詞「ABC」,然後「A」,「B」,「C」將與myDict的「鑰匙」相交,結果打印出將包括所有myDict的關鍵字'a','b','c'中的值。

編輯:所謂 「交叉點」 在這裏我的意思是2套(wordFind和myDict)之間的交叉點 - 希望這足夠清楚..

到目前爲止我的代碼是:

n=open("a7.txt","r") 

line=n.readlines() 

myDict={} 
def myDict(): 
    for word in line: 
     word=word.strip().lower() 
     if word in myDict: 
      myDict[word[0]].append(word) 
     else: 
      myDict[word[0]]=word 


wordFind=("Enter word: ") 
wordFind=wordFind.lower() 
wordFind=set(wordFind) 

# I get stuck at this second part, which requires me to use intersection 
temp={} 
for word in wordFind: 
    temp= word.intersection(myDict) 
    print(temp) 

    n.close() 

但我得到這個錯誤:

Traceback (most recent call last): 
    File "/Users/annie_mabu/Documents/CS/bt2.py", line 21, in <module> 
    temp= word.intersection(myDict) 
AttributeError: 'str' object has no attribute 'intersection' 

任何人都可以告訴我我哪裏犯了錯誤,以及如何解決它?

+1

我不明白你所說的「交集」的意思是什麼,不喜歡猜測。請擴展你的解釋(編輯你的問題,而不是在這裏的意見)。 – martineau 2015-04-04 20:05:18

+0

「wordFind」或「myDict」都沒有設置,因此「十字路口」令人困惑。你的意思是會員嗎? – dawg 2015-04-05 00:59:27

回答

0

我需要的文本文件轉換成字典,如:myDict = {「一」:'所有的字開始A ' 'B':所有的字開始B',等等}

我想使用此功能啓動:

def make_dict(): 
    d = {} 
    with open("a7.txt","r") as wordfile: 
     for word in wordfile: 
      word = word.strip().lower() 
      first = word[0] 
      if first not in d: d[first] = [] 
      d[first].append(word) 
    return d 

myDict = make_dict() 

然後我用wordFind =(「輸入單詞: 「),當我輸入一個與myDict中的鍵值相交的wordFind時,結果會給我該交集中的所有值。

如果你只是想與相同字母如您輸入的字詞,然後像開頭的單詞的列表:

wordFind = raw_input("Enter word: ") # raw_input for Python2, input for Python3 
wordFind = wordFind.lower() 
find_first = wordFind[0] 
matches = myDict[find_first] 
print(matches) 

應該工作。

如果你想更廣泛的匹配,這樣的匹配需要開始用同一套您輸入,然後像字符的話:

wordFind = raw_input("Enter word: ") # raw_input for Python2, input for Python3 
wordFind = wordFind.lower() 
find_first = wordFind[0] 
matches = [w for w myDict[find_first] if w.startswith(wordFind)] # This is different 
print(matches) 

應該工作。

編輯:每評論:

對於輸入「ABC」,如果你想要的所有以「A」,「B」或「C」,事遂所願開始單詞列表下面應該工作:

wordFind = raw_input("Enter word: ") # raw_input for Python2, input for Python3 
wordFind = wordFind.lower() 
matches = [] 
for c in wordFind: matches.extend(myDict[c]) 
print(matches) 

如果你希望他們分開,而不是在一個列表中,你可以這樣做:

​​
+0

感謝您的回覆。第一個代碼正是我想要構建myDict的。但是第二部分並不真正在尋找什麼。就像我在我的問題中編輯的那樣,當我輸入「abc」時,結果會打印出'a','b','c'中的所有值,而不是隻輸出'a'... – Tracey 2015-04-04 23:54:53

+1

謝謝你終於工作:) – Tracey 2015-04-05 01:16:52

1

剛剛看了一個清單,每首字母鍵:

with open(ur_file) as f: 
    d={} 
    for word in f: 
     d.setdefault(word[0].lower(), []).append(word) 

然後你有一個字典,像這樣:

>>> d 
{'a': ['a', 'aaoo', 'aloor', 'azur'], 'c': ['church', 'croccoli'], 'b': ['black', 'blue'], 'd': ['dark', 'den'], 'z': ['zic', 'zip']} 

然後,你可以寫一個簡單的功能,找到你的話:或者,如果您知道自己的文件中包含全部26個字母,則可以將全部26個字母設置爲開始Wii日空列表:

d={k:list() for k in 'abcdefghijklmnopqrstuvwxyz'} 
with open(ur_file) as f: 
    for word in f: 
     d[word[0].lower()].append(word) 

通過 '.intersection' 你可能會想的sets

>>> set(['a', 'aaoo', 'aloor', 'azur']).intersection(set(['art'])) 
set([]) 
>>> set(['a', 'aaoo', 'aloor', 'azur']).intersection(set(['aaoo'])) 
set(['aaoo']) 

然而,無論你有一個列表,字典,設置,串 - 該in關鍵字最好的測試成員的單個元素:

>>> 'art' in set(['a', 'aaoo', 'aloor', 'azur']) 
False 
>>> 'azur' in set(['a', 'aaoo', 'aloor', 'azur']) 
True 
+1

對於你的問題的第二部分,你可以使用'if [d [word] [0]]中的單詞'而不是'if d'中的單詞' – dylrei 2015-04-04 19:08:11

+0

感謝您的回覆。 – Tracey 2015-04-04 22:27:08

+0

但我應該在第二部分中使用十字路口來找出我發現哪個字母與d(鍵)相交,然後打印出值 – Tracey 2015-04-04 22:28:07

0

您可能要擴大你通過「路口」的意思,但試試這個:

words = [w.strip().lower() for w in open("a7.txt").read().splitlines()] 

word_dict = {} 
for word in words: 
    if word[0] in word_dict: 
     word_dict[word[0]].append(word) 
    else: 
     word_dict[word[0]] = [word] 

wordFind = raw_input("Enter word: ").strip().lower() 
print '\n'.join(word_dict[wordFind[0]])