我可以帮你翻译这句话:如何获取数组的内容

huangapple go评论83阅读模式
英文:

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]) -> dictionary

Without 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']]
>>> 

huangapple
  • 本文由 发表于 2020年1月3日 21:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定