2

ある種の論理エラーが発生しており、それを特定できないようです。ここに私が持っているものがあります:

Document = 'Sample1'
locationslist = []
thedictionary = []
userword = ['the', 'a']
filename = 'Sample1'
for inneritem in userword:
     thedictionary.append((inneritem,locationslist))
     for position, item in enumerate(file_contents): 
        if item == inneritem:
            locationslist.append(position)
wordlist = (thedictionary, Document)
print wordlist

したがって、基本的に、特定のユーザーワードと一緒に小さなリスト (locationslist) から大きなリスト (thedictionary) を作成しようとしています。出力がすべての単語のすべての場所をすべてのリストに入れていることを除いて、私はほとんどそれを持っています(2つしかありません -'the''a')。単純なロジックの問題があるようですが、見つけられないようです。出力は次のとおりです。

([('the', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161, 2, 49, 57, 131, 167, 189, 194, 207, 215, 224]), 
  ('a', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161, 2, 49, 57, 131, 167, 189, 194, 207, 215, 224])], 
 'Sample1')

しかし、次のようにする必要があります。

([('the', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161]), 
  ('a', [2, 49, 57, 131, 167, 189, 194, 207, 215, 224])], 
 'Sample1')

'the'各ユーザーワードと'a'?に関する問題のある出力のそれぞれに、両方の位置リストがどのように追加されているかを確認してください。ここで間違っていることについてアドバイスを使用できます..

4

2 に答える 2

3

作成するのは 1 つlocationslistだけなので、所有するのは 1 つだけです。それは両方の言葉で共有されています。locationslistループの反復ごとに新しいものを作成する必要があります。

for inneritem in userword:
    locationslist = []
    thedictionary.append((inneritem,locationslist))
    # etc.
于 2013-04-11T19:12:59.443 に答える