如何读取多数组的代码输出这样的格式

时间:2017-05-19 18:55:18

标签: python list

groups = [["Roy","Sam","Amy"],["Tom","Jerry"]]

尝试理解我从此代码中获得的输出

for group in groups: 
  for name in group: 
     print(group)

['Roy', 'Sam', 'Amy']
['Roy', 'Sam', 'Amy']
['Roy', 'Sam', 'Amy']
['Tom', 'Jerry']
['Tom', 'Jerry']

1 个答案:

答案 0 :(得分:1)

好像你打算打印name(内部列表中的元素),而不是group(内部列表本身):

>>> groups = [["Roy","Sam","Amy"],["Tom","Jerry"]]
>>> for group in groups: 
...     for name in group:
...             print(name)
... 
Roy
Sam
Amy
Tom
Jerry