I am coding in python and ....
我在python和....编码
I have a quick question. I am trying to reset the values of a global array by calling a certain function but am having difficulty. Here is my code at the moment:
我有个问题。我试图通过调用某个函数来重置全局数组的值,但遇到了困难。以下是我目前的密码:
CHOICES = (('1', 'First'), ('2', 'Second'))
def set_choices():
global CHOICES
CHOICES = (('3', 'Third'), ('4', 'Fourth'))
Essentially what I want to do is reset the array CHOICES by calling the function from some other function. Is there a way to do that?
本质上,我要做的是通过从其他函数调用函数来重置数组选项。有办法做到吗?
Thanks!
谢谢!
2 个解决方案
#1
3
myObject = [('1', 'First'), ('2', 'Second')]
CHOICES = set(myObject)
def set_choices():
global CHOICES
CHOICES.clear() # Remove the element from set CHOICES
# Do some of your changes here
anotherObject = [('3', 'Third'), ('4', 'Fourth')]
CHOICES[:] = set(anotherObject)
print(CHOICES) # Before calling set_choices
set_choices()
print(CHOICES) # After you calling set_choices
I think this will work. But I don't know if using set and tuple is a good idea, I personally would suggestion you to use list of list instead. Are there particular reason to use a set instead of other options?
我想这行得通。但是我不知道用set和tuple是不是一个好主意,我个人建议你用list代替。是否有特别的理由使用集合而不是其他选项?
Output:
输出:
{('2', 'Second'), ('1', 'First')}
{('4', 'Fourth'), ('3', 'Third')}
Respond to your comment to use list:
回复你的评论使用列表:
CHOICES = [['1', 'First'], ['2', 'Second']]
def set_choices():
# Changed since the comment of another member aaronasterling
# Removed the use of global
CHOICES[:] = [['3', 'Third'], ['4', 'Fourth']]
print(CHOICES)
set_choices()
print(CHOICES)
Output:
输出:
[['1', 'First'], ['2', 'Second']]
[['3', 'Third'], ['4', 'Fourth']]
To learn more about slice assignment, check out this SO question & answer.
要了解更多关于片赋值的信息,请查看“所以问题和答案”。
#2
1
If you want to do this with a list then there's no need for the global
keyword.
如果您想要使用列表来完成此操作,那么不需要使用全局关键字。
CHOICES = [('1', 'First'), ('2', 'Second')
def set_choices():
CHOICES[:] = (('3', 'Third'), ('4', 'Fourth'))
This will replace the content of the list without changing the reference. It works by slice assignment. The CHOICES[:]
references a slice of the whole list.
这将在不更改引用的情况下替换列表的内容。它的工作原理是切片作业。选项[:]引用整个列表的一部分。
#1
3
myObject = [('1', 'First'), ('2', 'Second')]
CHOICES = set(myObject)
def set_choices():
global CHOICES
CHOICES.clear() # Remove the element from set CHOICES
# Do some of your changes here
anotherObject = [('3', 'Third'), ('4', 'Fourth')]
CHOICES[:] = set(anotherObject)
print(CHOICES) # Before calling set_choices
set_choices()
print(CHOICES) # After you calling set_choices
I think this will work. But I don't know if using set and tuple is a good idea, I personally would suggestion you to use list of list instead. Are there particular reason to use a set instead of other options?
我想这行得通。但是我不知道用set和tuple是不是一个好主意,我个人建议你用list代替。是否有特别的理由使用集合而不是其他选项?
Output:
输出:
{('2', 'Second'), ('1', 'First')}
{('4', 'Fourth'), ('3', 'Third')}
Respond to your comment to use list:
回复你的评论使用列表:
CHOICES = [['1', 'First'], ['2', 'Second']]
def set_choices():
# Changed since the comment of another member aaronasterling
# Removed the use of global
CHOICES[:] = [['3', 'Third'], ['4', 'Fourth']]
print(CHOICES)
set_choices()
print(CHOICES)
Output:
输出:
[['1', 'First'], ['2', 'Second']]
[['3', 'Third'], ['4', 'Fourth']]
To learn more about slice assignment, check out this SO question & answer.
要了解更多关于片赋值的信息,请查看“所以问题和答案”。
#2
1
If you want to do this with a list then there's no need for the global
keyword.
如果您想要使用列表来完成此操作,那么不需要使用全局关键字。
CHOICES = [('1', 'First'), ('2', 'Second')
def set_choices():
CHOICES[:] = (('3', 'Third'), ('4', 'Fourth'))
This will replace the content of the list without changing the reference. It works by slice assignment. The CHOICES[:]
references a slice of the whole list.
这将在不更改引用的情况下替换列表的内容。它的工作原理是切片作业。选项[:]引用整个列表的一部分。