我有一個函數all_word_pairs(它從同一目錄中的所有.txt文件獲取所有單詞,並創建一個列表包含兩個元素的列表:這個詞本身以及其起源的文件),它的定義是這樣的:Python 3.x:我的函數返回'TypeError:類型'函數'的對象沒有len()'
def all_word_pairs():
"""Return list of word + filename tuples from .txt files in current directory"""
# collect .txt filenames
filenames = []
# scan all files in current directory
for f in os.listdir("."):
# test if f ends with .txt
if f.endswith(".txt"):
# yes it does, so add it to filenames
filenames.append(f)
# collect word pairs from all filenames
return word_pairs(filenames)
This is what print(all_word_pairs()) will return.
現在我建立一個函數在給定的列表中的列表進行排序( all_word_pairs函數)的第一個元素(檢索到的字):
def merge_pairs(data):
if len(data) <= 1:
res = data
return res
else:
res = []
fi = 0
si = 0
imid = len(data) // 2
fst = merge_pairs(data[:imid])
snd = merge_pairs(data[imid:])
while fi <= len(fst)-1 and si <= len(snd) - 1:
if fst[fi][0] < snd[si][0]:
res.append(fst[fi])
fi = fi + 1
else:
res.append(snd[si])
si = si + 1
if fi <= len(fst) - 1:
res.extend(fst[fi:])
elif si <= len(snd) - 1:
res.extend(snd[si:])
i = 0
while i < len(res) - 1:
while res[i][1] == res[i + 1][1]:
res[i], res[i + 1] = res[i + 1], res[i]
i = i + 1
return res
當我調用函數merge_pairs時,它給了我一個TypeError:objectof type'function'沒有len()。 This is what it looks like.
有誰知道出了什麼問題?我非常感謝你的幫助!
要調用'merge_pairs'帶*的功能'all_word_pairs' *作爲參數,作爲錯誤信息告訴你。 – jonrsharpe 2014-09-10 22:11:26