我有一个函数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