简体   繁体   English

如何从 function 导出结果? (更新了更多代码)

[英]How to export a result from function? (updated with more code)

Stackoverflow, hello.堆栈溢出,你好。 I didn't found any information about exporting a data (list in my case) from a "function"我没有找到有关从“函数”导出数据(在我的情况下为列表)的任何信息

The end of my code is:我的代码的结尾是:

def namesearch():
    with open ('recipes.txt') as f:
        dishnames = []
        for line in f:
            line = line.strip()
            # print (line)
            line2=line.split()
            if len(line2) <= 3:
                pass
                try:
                    a=(int(line2[0])/1)
                except IndexError:
                    pass
                except ValueError:
                    dishnames.append(line2)
        for i in dishnames:
            if len(i) > 1:
                j=' '.join(i)
            else:
                j = str(i[0])
            dishnames_string.append(j)

namesearch()

def search_ingridientsnames():
    with open('recipes.txt') as f:
        ingridient_names = []
        for line in f:
            line = line.strip()
            # print (line)
            line2 = line.split()
            try:
                i=0
                if len(line2) > 3 or len(line2)<1:
                    ingridient_names.append(line2[0])
            except IndexError:
                ingridient_names.append('_')
        # print(ingridient_names)
    ingridient_names_final=[i.split(',') for i in ','.join(ingridient_names).split(',_,')]
print(ingridient_names_final)
search_ingridientsnames()

print (dishnames_string)
print (ingridient_names_final)

The result is结果是

>> [['Egg', 'Milk', 'Tomato'], ['Duck', 'Water', 'Honey', 'Soy'], ['Potato', 'Garlic', 'Gouda'], ['Beef', 'Sweet', 'Pita', 'Wine', 'Tomato']]
>> ['Omelette', 'Peking Duck', 'Baked potato', 'Fajitos']
[]

Do you see that?你看到了吗? The first print(ingridient_names_final) in the end of function - writing the list from function and it's ok. function 末尾的第一个print(ingridient_names_final) - 从 function 写入列表,没关系。 As you see, I have another value from function, print (dishnames_string) , which is also a part of function - the result of the work is the list.如您所见,我还有来自 function, print (dishnames_string)的另一个值,它也是 function 的一部分 - 工作的结果是列表。 However, when I print again the value from function print (search_ingridientsnames) - the result is [].但是,当我再次打印 function print (search_ingridientsnames)中的值时 - 结果是 []。 And I even can't write print(list(search_ingridientsnames)) - the result is an error.而且我什至不能写 print(list(search_ingridientsnames)) - 结果是一个错误。 How could I recieve the same result from function launching?我怎样才能从 function 启动中收到相同的结果?

The result should be:结果应该是:

>> [['Egg', 'Milk', 'Tomato'], ['Duck', 'Water', 'Honey', 'Soy'], ['Potato', 'Garlic', 'Gouda'], ['Beef', 'Sweet', 'Pita', 'Wine', 'Tomato']]
>> ['Omelette', 'Peking Duck', 'Baked potato', 'Fajitos']
>> [['Egg', 'Milk', 'Tomato'], ['Duck', 'Water', 'Honey', 'Soy'], ['Potato', 'Garlic', 'Gouda'], ['Beef', 'Sweet', 'Pita', 'Wine', 'Tomato']]

UPD UPD

但是,如果我尝试在每个人的帮助下输入 print (search_ingridientsnames()),结果是所需的列表和之后的“NONE”。怎么回事?

The code is printing exactly what you asked for.该代码正在打印您所要求的内容。 To print the result of the function you need to use:要打印 function 的结果,您需要使用:

print (search_ingridientsnames())

From a first hand look i can say that python is interpreting the result of the print statement as an object and is therefore printing what you see on the console.从第一手的角度来看,我可以说 python 正在将打印语句的结果解释为 object,因此正在打印您在控制台上看到的内容。 You need to write it as print(search_ingredientsname()).您需要将其写为 print(search_ingredientsname())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM