如何将打印函数的结果添加到列表中
How do I add the result of a print function ito a list
我有以下以打印函数结尾的定义:
from nltk.corpus import words
nltk.download('words')
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result = print(sorted(temp, key = lambda val:val[0])[0][1])
return result
answer_nine()
我已正确打印出三个结果,但我想将它们列在一个列表中。我试图以多种不同的方式将它们分配到列表中,但我总是收到以下错误消息:AttributeError: 'NoneType' object has no attribute 'append'. I do不明白为什么我的结果有一个 NoneType 如果它有值,我在这里遗漏了什么?
ps.: 如果我像这样删除打印功能: result = sorted(temp, key = lambda val:val[0])[0][1]
我只收到第三个单词,但至少它有字符串类型。
from nltk.corpus import words
import nltk
nltk.download('words')
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
result_2=[]
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result_2.append(sorted(temp, key = lambda val:val[0])[0][1])
result = print(sorted(temp, key = lambda val:val[0])[0][1])
return result, result_2
a,b=answer_nine()
print(a)
print(b)
这段代码对我有用!并在列表中给出来自函数调用的相同字符串,除了 None 值
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
result = []
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result.append(sorted(temp, key = lambda val:val[0])[0][1])
return result
returns ['corpulent', 'indecence', 'validate']
我有以下以打印函数结尾的定义:
from nltk.corpus import words
nltk.download('words')
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result = print(sorted(temp, key = lambda val:val[0])[0][1])
return result
answer_nine()
我已正确打印出三个结果,但我想将它们列在一个列表中。我试图以多种不同的方式将它们分配到列表中,但我总是收到以下错误消息:AttributeError: 'NoneType' object has no attribute 'append'. I do不明白为什么我的结果有一个 NoneType 如果它有值,我在这里遗漏了什么?
ps.: 如果我像这样删除打印功能: result = sorted(temp, key = lambda val:val[0])[0][1]
我只收到第三个单词,但至少它有字符串类型。
from nltk.corpus import words
import nltk
nltk.download('words')
correct_spellings = words.words()
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from nltk.metrics.distance import edit_distance
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
result_2=[]
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result_2.append(sorted(temp, key = lambda val:val[0])[0][1])
result = print(sorted(temp, key = lambda val:val[0])[0][1])
return result, result_2
a,b=answer_nine()
print(a)
print(b)
这段代码对我有用!并在列表中给出来自函数调用的相同字符串,除了 None 值
def answer_nine(entries=['cormulent', 'incendenece', 'validrate']):
result = []
for entry in entries:
temp = [(jaccard_distance(set(ngrams(entry, 2)), set(ngrams(w, 2))),w) for w in correct_spellings if w[0]==entry[0]]
result.append(sorted(temp, key = lambda val:val[0])[0][1])
return result
returns ['corpulent', 'indecence', 'validate']