如何在python中的函数外部调用列表? [英] How do I call a list outside of a function in python?

查看:99
本文介绍了如何在python中的函数外部调用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def start(B):
    wordlist = []

    for w in B:
        content = w
        words = content.lower().split()
        for each_word in words:

            wordlist.append(each_word)
            print(each_word)
            return(wordlist)

当我将列表称为"wordlist"时,它返回列表中没有任何内容.由于列表在函数内部起作用,因此如何使列表在函数外部可调用.

When I call list 'wordlist' it returns that there isn't anything inside the list. How do I get the list to be callable outside of the function since it works inside the function.

谢谢,我已经更新了代码以反映我使用打印标签而不是返回标签所犯的错误.

Thank you I have updated the code to reflect the mistake I was making using a print tag instead of a return tag.

推荐答案

def start(B):
    wordlist = []

    for w in B:
        content = w
        words = content.lower().split()
        for each_word in words:

            wordlist.append(each_word)
            print(wordlist)
    return wordlist

B=["hello bye poop"]
wordlist=start(B)

只需将返回单词列表添加到函数中即可.每当在函数中适当调用函数时,在函数中添加 return 语句都会返回该对象,并且您可以将返回的变量存储在全局范围变量中.

Just add return wordlist to the function. Adding a return statement in a function returns the object whenever the function is called appropriately and you can store that returned variable in a global scope variable.

这篇关于如何在python中的函数外部调用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆