我开发了一个应该返回字典列表的函数。但是,它会返回None
。当我测试代码返回其他东西时,它工作正常。为什么词典列表失败?
相关代码:
def FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries = [], iteration = 0):
iteration += 1
if iteration > maxIteration:
print dictionaries
return dictionaries
#Other code here
dicts = FindCommonPhrases (phrases, listOfWords, numWords)
print dicts
结果:
[{u'REPAIR': 4397, u'RENEWED': 192, u'212': 398, u'COUPLI': 5411, u'TR': 3357, u'OVER': 304, u'2PL': 2972, u'BJ': 1677, u'1OF': 130, u'STREET': 354, u'ELBOW': 188, u'CONT': 530, u'LINE': 275, u'WITH': 5019, u'BY': 1052, u'24': 483, u'STUB': 233, u'PIECE': 125, u'20': 234, u'COUPLING': 5146, u'RR': 730, u'WAS': 132, u'METER': 458, u'RETHREADED': 121, u'12CU': 327, u'4': 10292, u'8': 3133, u'REP': 25908, u'CLAMP': 596, u'DAMAGED': 918, u'FULL': 172, u'OFF': 524, u'FIT': 2088, u'COUPLIN': 5282, u'SVC': 4649, u'15': 252, u'P': 39866, u'DEAD': 302, u'RERUN': 419, u'X': 1227, u'THE': 284, u'TRANS': 445, u'34STEEL': 181, u'ON': 9645, u'BROKEN': 352, u'OF': 12452, u'14PL': 342, u'W12': 459, u'KIT': 438, u'JOINT': 1068, u'LOWERED': 196, u'W2': 313, u'W1': 737, u'METFIT': 299, u'CIRCLE': 162, u'MAIN': 3127, u'COUP': 6916, u'USED': 456, u'COUPL': 5760, u'BELL': 1183, u'HP': 202, u'CHANGED': 142, u'PLASTIC': 4135, u'DOWN': 333, u'114': 307, u'PERFECTION': 1330, u'M2': 382, u'COUPS': 315, u'AVON': 146, u'INSERTED': 4283, u'SERV': 9149, u'SERT': 7458, u'MADE': 141, u'CRACKED': 135, u'RERAN': 1836, u'ALDYL': 447, u'PB': 413, u'34STL': 257, u'3': 5758, u'7': 650, u'AMP': 873, u'PERMA': 3781, u'PL': 31660, u'PLUG': 216, u'C': 37103, u'END': 450, u'PERMABOND': 2269, u'FOR': 245, u'GREASED': 107, u'PERMACERT': 109, u'CORRODED': 138, u'W': 11440, u'GM': 220, u'PERF': 1910, u'AND': 3128, u'22': 334, u'LEAK': 2218, u'30': 315, u'ELECTROFUSION': 258, u'34': 2849, u'INSTD': 660, u'INSTL': 208, u'RISER': 3760, u'REMOVED': 265, u'REPAIRED': 2581, u'STEEL': 1397, u'COU': 7061, u'SERVICE': 8222, u'SECTION': 1086, u'VALVE': 2088, u'STOP': 361, u'REPR': 183, u'2OF': 208, u'12PL': 2669, u'FUSION': 896, u'REPL': 20704, u'CURB': 176, u'PERM': 4024, u'SERVICES': 182, u'NEW': 1791, u'REBUILT': 248, u'ELECTRO': 550, u'REG': 268, u'SET': 504, u'OLD': 358, u'SER': 18071, u'LEAKING': 1335, u'TEE': 3989, u'REPLACE': 16472, u'PIPE': 6570, u'2': 19616, u'REPLACED': 14670, u'6': 2146, u'COCK': 348, u'LEAKAGE': 140, u'OUT': 755, u'CI': 1599, u'CUT': 1339, u'CO': 9649, u'ABANDONED': 132, u'DUE': 245, u'FITTING': 966, u'ALDYLA': 168, u'EXISTING': 159, u'TR418': 1346, u'CU': 2726, u'PERMASERT': 937, u'10': 504, u'12': 12120, u'FITTINGS': 287, u'14': 3276, u'STOPCOCK': 163, u'16': 533, u'18': 2266, u'TRANSITION': 245, u'WELDED': 409, u'ST': 17811, u'MET': 1321, u'TIGHTENED': 165, u'BRANCH': 430, u'SQUEEZED': 225, u'USING': 754, u'SEAL': 365, u'REPAIRS': 182, u'1PL': 396, u'CAP': 1036, u'TWO': 822, u'BAD': 123, u'INST': 10214, u'INSTALL': 5966, u'CREW': 169, u'COUPLINGS': 3255, u'PREFAB': 244, u'INSERT': 6324, u'WELD': 633, u'12PLASTIC': 133, u'GAS': 1936, u'ONE': 600, u'1': 20805, u'TO': 3334, u'5': 1676, u'LP': 482, u'A': 37589, u'COPPER': 625, u'STL': 2294, u'INTO': 316, u'HEAT': 168, u'AT': 1612, u'IN': 24552, u'FOUND': 150, u'112': 305, u'125': 134, u'INSERTING': 175, u'FROM': 408, u'SLEEVE': 233, u'PERMABONDED': 1317, u'INSTALLED': 5239, u'CILP': 207, u'21': 666, u'T': 31607}]
None
测试代码:
def FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries = [], iteration = 0):
iteration += 1
if iteration: #> maxIteration:
print dictionaries
return dictionaries
#Other code here
dicts = FindCommonPhrases (phrases, listOfWords, numWords)
print dicts
测试结果:
[]
[]
出了什么问题?
更相关的代码:
phrases = ["phrase", "another phrase...", "etc etc"]
listOfWords = ["word1", "another", "word", "etc"]
numWords = 2
def FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries = [], iteration = 0):
iteration += 1
if iteration > maxIteration:
print dictionaries
return dictionaries
liCombos = list(itertools.combinations(liWords, iteration))
di = {}
for liCheck in liCombos:
phrase = " ".join (liCheck)
for checkPhrase in liPhrases:
if not phrase in checkPhrase:
continue
if phrase in di:
di[phrase] += 1
else:
di[phrase] = 1
dictionaries += [di]
FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries, iteration)
dicts = FindCommonPhrases (phrases, listOfWords, numWords)
print dicts
答案 0 :(得分:1)
您正在以递归方式调用该函数,但不会从递归调用返回结果。
def FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries = [], iteration = 0):
iteration += 1
if iteration > maxIteration:
return dictionaries
# more code here
FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries, iteration)
将最后一行更改为return
结果,然后它应该有效。
return FindCommonPhrases (liPhrases, liWords, maxIteration, dictionaries, iteration)
另请注意,使用类似dictionaries = []
的默认参数通常是一个坏主意,因为对于每次调用函数,这将使用相同的列表!看看如果你两次调用你的功能会发生什么。试试这个:
def FindCommonPhrases(liPhrases, liWords, maxIteration, dictionaries=None, iteration=0):
if dictionaries is None:
dictionaries = []
# more stuff