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

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

How can I take the content of array

问题

Sure, here is the translated code:

  1. 我想知道如何将以yoyo开头的几个数组的值添加到一个矩阵中
  2. 代码:
  3. yoyo_asd=('a','b','c')
  4. yoyo_bsd=('asd','111','222')
  5. matrix=[]
  6. 我尝试了类似这样的方法但没有成功:
  7. for vars in dir():
  8. if vars.startswith("yoyo"):
  9. matrix.append([vars])
  10. 这是我的目标:
  11. matrix = [
  12. ['a','b','c'],
  13. ['asd','111','222'],
  14. ]

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:

  1. yoyo_asd=('a','b','c')
  2. yoyo_bsd=('asd','111','222')
  3. matrix=[]

I am trying with something like this but nothing..

  1. for vars in dir():
  2. if vars.startswith("yoyo"):
  3. matrix.append([vars])

This is my goal:

  1. matrix = [
  2. ['a','b','c'],
  3. ['asd','111','222'],
  4. ]

答案1

得分: 4

matrix = [list(val) for key, val in globals().items() if key.startswith('yoyo')]

英文:

Should be a one-liner:

  1. 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:

  1. >>> yoyo_asd=('a','b','c')
  2. >>> yoyo_bsd=('asd','111','222')
  3. >>> matrix=[]
  4. >>> for key, val in vars().items(): # if you are want a global namespace, then use `globals()`
  5. ... if key.startswith('yoyo'):
  6. ... matrix.append(val)
  7. ...
  8. >>> matrix
  9. [('a', 'b', 'c'), ('asd', '111', '222')]

Help on built-in function vars in module builtins:

vars(...)
vars([object]) -> dictionary

  1. Without arguments, equivalent to locals().
  2. With an argument, equivalent to object.__dict__.
英文:

You could just use the vars which returns a dict of the local namespace like:

  1. >>> yoyo_asd=('a','b','c')
  2. >>> yoyo_bsd=('asd','111','222')
  3. >>> matrix=[]
  4. >>> for key, val in vars().items(): # if you are want a global namespace, then use `globals()`
  5. ... if key.startswith('yoyo'):
  6. ... matrix.append(val)
  7. ...
  8. >>> matrix
  9. [('a', 'b', 'c'), ('asd', '111', '222')]
  10. >>>

> 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

尝试像这样,你想要的我会按照这种方式工作。

  1. yoyo_asd=('a','b','c')
  2. yoyo_bsd=('asd','111','222')
  3. matrix=[]
  4. for vars in dir():
  5. if vars.startswith("yoyo"):
  6. matrix.append([i for i in vars])
英文:

Try like this what u want i would work like that.

  1. yoyo_asd=('a','b','c')
  2. yoyo_bsd=('asd','111','222')
  3. matrix=[]
  4. for vars in dir():
  5. if vars.startswith("yoyo"):
  6. matrix.append([i for i in vars])

答案4

得分: 0

将元组转换为列表,并迭代以附加到矩阵。

  1. matrix.append(list(yoyo_asd))
  2. matrix.append(list(yoyo_bsd))
  1. >>> yoyo_asd = ('a', 'b', 'c')
  2. >>> yoyo_bsd = ('asd', '111', '222')
  3. >>> matrix = []
  4. >>> matrix.append(list(yoyo_asd))
  5. >>> matrix.append(list(yoyo_bsd))
  6. >>> matrix
  7. [[a, b, c], [asd, 111, 222]]
  8. >>>
英文:

convert tuple to list and iterate over to append to matrix.

  1. matrix.append(list(yoyo_asd))
  2. matrix.append(list(yoyo_bsd))
  3. >>> yoyo_asd=('a','b','c')
  4. >>> yoyo_bsd=('asd','111','222')
  5. >>> matrix=[]
  6. >>> matrix.append(list(yoyo_asd))
  7. >>> matrix.append(list(yoyo_bsd))
  8. >>> matrix
  9. [['a', 'b', 'c'], ['asd', '111', '222']]
  10. >>>

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:

确定