简体   繁体   English

在函数之间传递列表

[英]Passing Lists between Functions

Background - I have created 2 functions as part of a project to make API calls using values from an entities_list and views_list .背景-我已经建立2个作为项目的一部分进行使用从值的API调用entities_listviews_list The first function def user_inputs() asks for 2 user inputs as integer values (with v.basic error handling).第一个函数def user_inputs()要求 2 个用户输入作为整数值(使用 v.basic 错误处理)。 The second function def url_constructor() iterates through the values in the 2x lists (using nested loops) and constructs URLs for my API call, before saving the URLs in the url_list , before a latter function will handle the API calls.第二个函数def url_constructor()遍历 2x 列表中的值(使用嵌套循环)并为我的 API 调用构造 URL,然后将 URL 保存在url_list ,然后后一个函数将处理 API 调用。

My issue - I am having issues accessing entities_list and views_list , specifically returning the lists from my def user_inputs() and having as arguments in the def url_constructor()我的问题-我有访问问题entities_listviews_list ,特别是从我返回列表def user_inputs()并具有如参数def url_constructor()

My code - you can see from this function, that I have attempted to return both lists respectively.我的代码 -你可以从这个函数中看到,我试图分别return两个列表。 Nothing breaks, and I can input my values into the list without issue -没有任何问题,我可以毫无问题地将我的值输入到列表中 -

def user_inputs():
    while True:
        try:
            entities_list = [int(x) for x in input("Entities for API Call:\n").split(', ')]
        except ValueError:
            print("---ERROR: MUST BE COMMA SEPERATED VALUES---")
            return entities_list
            continue
        break
    while True:
        try:
            views_list = [int(x) for x in input("Views for API Call:\n").split(', ')]
        except ValueError:
            print("---ERROR: MUST BE COMMA SEPERATED VALUES---")
            return views_list
            continue
        break
    return entities_list, views_list
user_inputs()

However, we running the following code block for my def user_inputs() , I get a NameError: name 'entities_list' is not defined -但是,我们为我的def user_inputs()运行以下代码块,我收到NameError: name 'entities_list' is not defined -

def url_constructor(entities_list, views_list):
    str = 'https://some_url/{}/view{}'
    for entity in entities_list:
        for view in views_list:
            url = str.format(entity, view)
            url_list.append(ur)
url_constructor(entities_list, views_list)

I am not looking for someone to do my work for me, however just guide me in the right direction.我不是在找人为我做我的工作,但只是引导我朝着正确的方向前进。 Your help is greatly appreciated!非常感谢您的帮助!

Look at the example below and try to apply to your code看看下面的例子并尝试应用到你的代码中

def foo():
  list_a = [5]
  list_b = [12]
  return list_a,list_b

def bar(lst):
  print(lst)

a,b = foo()
bar(a)

I think you need:我认为你需要:

entities_list, views_list = user_inputs()

otherwise, the values returned from the user_inputs function get thrown away because they aren't assigned to anything.否则,从 user_inputs 函数返回的值会被丢弃,因为它们没有分配给任何东西。

Protip: you could also do: Protip:你也可以这样做:

url_constructor(*user_inputs())

https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists

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

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