Python 函数和列表,它们需要放在哪里?
Python functions and lists, where do they need to be?
对 Python 还很陌生,所以这可能是基础知识,但非常感谢您的帮助。我正在使用 Python 调用我的 Tableau 服务器 API,并尝试循环浏览组中的每个用户并构建一个电子邮件地址列表,目的是通过电子邮件向每个组发送他们观点的 pdf 文件。我已经在所有函数之外定义了我需要的列表(我认为这被称为全局?)。在下面的代码示例中,我将 'testuser' 附加到列表中,因为我不希望前面的循环最初起作用。我只是想确定数据将附加到全局定义的列表中。它不是。当我在主函数中打印列表时,它是空的。
xmlns = {'t': 'http://tableau.com/api'}
groupnames = ['Data Source Publisher']
workbooks = []
usernames = []
def get_email_address(groupname):
groups = server_response.findall('.//t:group', namespaces=xmlns)
for group in groups:
if group.get('name') == groupname:
usernames.append(group.get('id'))
usernames.append('2002327')
return usernames;
def main():
print(usernames)
if __name__ == '__main__':
main()
我这里的主要故障是什么?
问题不在于您没有更新全局变量。问题是您在代码中的任何时候都没有调用 get_email_address
。
- 我不能运行
groups = server_response.findall('.//t:group', namespaces=xmlns)
所以我必须猜测它的结构returns
- 我调用
get_email_address
时使用了一个我知道无法在 if group.get('name') == groupname:
中匹配的参数,因此您在检查后的默认 append
仍然会触发。
这符合您的预期:
xmlns = {'t': 'http://tableau.com/api'}
groupnames = ['Data Source Publisher']
workbooks = []
usernames = []
def get_email_address(groupname):
groups = [{'name': 'abc', 'id': '789'}, {'name': '123', 'id': '678'}] # I assume this is representative
for group in groups:
if group.get('name') == groupname:
usernames.append(group.get('id'))
usernames.append('2002327')
return usernames
def main():
get_email_address('something') # Call the function
print(usernames)
if __name__ == '__main__':
main()
如果您将 get_email_address('something')
更改为 get_email_address('abc')
那么您会注意到打印的全局列表包含两个项目(因为它会在 groups
中找到匹配项)。
对 Python 还很陌生,所以这可能是基础知识,但非常感谢您的帮助。我正在使用 Python 调用我的 Tableau 服务器 API,并尝试循环浏览组中的每个用户并构建一个电子邮件地址列表,目的是通过电子邮件向每个组发送他们观点的 pdf 文件。我已经在所有函数之外定义了我需要的列表(我认为这被称为全局?)。在下面的代码示例中,我将 'testuser' 附加到列表中,因为我不希望前面的循环最初起作用。我只是想确定数据将附加到全局定义的列表中。它不是。当我在主函数中打印列表时,它是空的。
xmlns = {'t': 'http://tableau.com/api'}
groupnames = ['Data Source Publisher']
workbooks = []
usernames = []
def get_email_address(groupname):
groups = server_response.findall('.//t:group', namespaces=xmlns)
for group in groups:
if group.get('name') == groupname:
usernames.append(group.get('id'))
usernames.append('2002327')
return usernames;
def main():
print(usernames)
if __name__ == '__main__':
main()
我这里的主要故障是什么?
问题不在于您没有更新全局变量。问题是您在代码中的任何时候都没有调用 get_email_address
。
- 我不能运行
groups = server_response.findall('.//t:group', namespaces=xmlns)
所以我必须猜测它的结构returns - 我调用
get_email_address
时使用了一个我知道无法在if group.get('name') == groupname:
中匹配的参数,因此您在检查后的默认append
仍然会触发。
这符合您的预期:
xmlns = {'t': 'http://tableau.com/api'}
groupnames = ['Data Source Publisher']
workbooks = []
usernames = []
def get_email_address(groupname):
groups = [{'name': 'abc', 'id': '789'}, {'name': '123', 'id': '678'}] # I assume this is representative
for group in groups:
if group.get('name') == groupname:
usernames.append(group.get('id'))
usernames.append('2002327')
return usernames
def main():
get_email_address('something') # Call the function
print(usernames)
if __name__ == '__main__':
main()
如果您将 get_email_address('something')
更改为 get_email_address('abc')
那么您会注意到打印的全局列表包含两个项目(因为它会在 groups
中找到匹配项)。