嵌套列表操作,需要双 forloop?
Nested list manipulation, double forloop needed?
所以我想改变这个嵌套(列表)让我们称之为 X
[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]
To This nested(list) 让它 Y
[['What if', 'Thing Explainer', 'Alan Turing: The Enigma'], [ 2014,2015,2014], ['Randall Munroe, Randall Munroe, 'Andrew Hodges']]
Y 中的第一项是 X 中第 i 个项的第一项。
['What if', 'Thing Explainer', 'Alan Turing: The Enigma']
Y 中的第二项是 X 中第 i 项的第二项
['Randall Munroe, Randall Munroe, 'Andrew Hodges']
任何人都可以在 python 中分享思考过程和解决方案吗?
您将要使用 Python 中的内置 zip
函数。
>>> zip(*[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']])
我不确定你想要什么包to/don不想使用。但是 numpy
可以很容易地做到这一点:
import numpy as np
dat = [['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]
np.array(dat).T.tolist()
# [['What if?', 'Thing Explainer', 'Alan Turing: The Enigma'],
# [' 2014', ' 2015', ' 2014'],
# [' Randall Munroe', ' Randall Munroe', ' Andrew Hodges']]
map (list, zip(*[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]))
这将完全满足您的需求。
所以我想改变这个嵌套(列表)让我们称之为 X
[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]
To This nested(list) 让它 Y
[['What if', 'Thing Explainer', 'Alan Turing: The Enigma'], [ 2014,2015,2014], ['Randall Munroe, Randall Munroe, 'Andrew Hodges']]
Y 中的第一项是 X 中第 i 个项的第一项。
['What if', 'Thing Explainer', 'Alan Turing: The Enigma']
Y 中的第二项是 X 中第 i 项的第二项
['Randall Munroe, Randall Munroe, 'Andrew Hodges']
任何人都可以在 python 中分享思考过程和解决方案吗?
您将要使用 Python 中的内置 zip
函数。
>>> zip(*[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']])
我不确定你想要什么包to/don不想使用。但是 numpy
可以很容易地做到这一点:
import numpy as np
dat = [['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]
np.array(dat).T.tolist()
# [['What if?', 'Thing Explainer', 'Alan Turing: The Enigma'],
# [' 2014', ' 2015', ' 2014'],
# [' Randall Munroe', ' Randall Munroe', ' Andrew Hodges']]
map (list, zip(*[['What if?', ' 2014', ' Randall Munroe'], ['Thing Explainer', ' 2015', ' Randall Munroe'], ['Alan Turing: The Enigma', ' 2014', ' Andrew Hodges']]))
这将完全满足您的需求。