英文:
How can I take the content of array
问题
Sure, here is the translated code:
我想知道如何将以“yoyo”开头的几个数组的值添加到一个矩阵中。
代码:
yoyo_asd=('a','b','c')
yoyo_bsd=('asd','111','222')
matrix=[]
我尝试了类似这样的方法但没有成功:
for vars in dir():
if vars.startswith("yoyo"):
matrix.append([vars])
这是我的目标:
matrix = [
['a','b','c'],
['asd','111','222'],
]
Please note that I've translated the code part as requested, and you can use this translated code for reference.
英文:
I would like to know how I add the values for a few arrays which start with "yoyo" in one matrix.
The code:
yoyo_asd=('a','b','c')
yoyo_bsd=('asd','111','222')
matrix=[]
I am trying with something like this but nothing..
for vars in dir():
if vars.startswith("yoyo"):
matrix.append([vars])
This is my goal:
matrix = [
['a','b','c'],
['asd','111','222'],
]
答案1
得分: 4
matrix = [list(val) for key, val in globals().items() if key.startswith('yoyo')]
英文:
Should be a one-liner:
matrix = [list(val) for key, val in globals().items() if key.startswith('yoyo')]
答案2
得分: 2
You could just use the vars
which returns a dict
of the local
namespace like:
>>> yoyo_asd=('a','b','c')
>>> yoyo_bsd=('asd','111','222')
>>> matrix=[]
>>> for key, val in vars().items(): # if you are want a global namespace, then use `globals()`
... if key.startswith('yoyo'):
... matrix.append(val)
...
>>> matrix
[('a', 'b', 'c'), ('asd', '111', '222')]
Help on built-in function vars in module builtins:
vars(...)
vars([object]) -> dictionaryWithout arguments, equivalent to locals(). With an argument, equivalent to object.__dict__.
英文:
You could just use the vars
which returns a dict
of the local
namespace like:
>>> yoyo_asd=('a','b','c')
>>> yoyo_bsd=('asd','111','222')
>>> matrix=[]
>>> for key, val in vars().items(): # if you are want a global namespace, then use `globals()`
... if key.startswith('yoyo'):
... matrix.append(val)
...
>>> matrix
[('a', 'b', 'c'), ('asd', '111', '222')]
>>>
> Help on built-in function vars in module builtins:
>
> vars(...)
> vars([object]) -> dictionary
>
> Without arguments, equivalent to locals().
> With an argument, equivalent to object.dict.
答案3
得分: 0
尝试像这样,你想要的我会按照这种方式工作。
yoyo_asd=('a','b','c')
yoyo_bsd=('asd','111','222')
matrix=[]
for vars in dir():
if vars.startswith("yoyo"):
matrix.append([i for i in vars])
英文:
Try like this what u want i would work like that.
yoyo_asd=('a','b','c')
yoyo_bsd=('asd','111','222')
matrix=[]
for vars in dir():
if vars.startswith("yoyo"):
matrix.append([i for i in vars])
答案4
得分: 0
将元组转换为列表,并迭代以附加到矩阵。
matrix.append(list(yoyo_asd))
matrix.append(list(yoyo_bsd))
>>> yoyo_asd = ('a', 'b', 'c')
>>> yoyo_bsd = ('asd', '111', '222')
>>> matrix = []
>>> matrix.append(list(yoyo_asd))
>>> matrix.append(list(yoyo_bsd))
>>> matrix
[[a, b, c], [asd, 111, 222]]
>>>
英文:
convert tuple to list and iterate over to append to matrix.
matrix.append(list(yoyo_asd))
matrix.append(list(yoyo_bsd))
>>> yoyo_asd=('a','b','c')
>>> yoyo_bsd=('asd','111','222')
>>> matrix=[]
>>> matrix.append(list(yoyo_asd))
>>> matrix.append(list(yoyo_bsd))
>>> matrix
[['a', 'b', 'c'], ['asd', '111', '222']]
>>>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论