gpt4 book ai didi

python - 如何拆分数组数组?

转载 作者:太空宇宙 更新时间:2023-11-04 09:30:03 25 4
gpt4 key购买 nike

我有一个包含 Python 中其他一些数组的数组,但我只需要主数组中每个迷你数组的第一个元素。有什么方法可以做到这一点吗?

例子:

array = [['a','1'], ['b','2'], ['c','3'], ['d','4'], ['e','5']]

我需要一行中的字母:

'a'
'b'
'c'
'd'
'e'

还有另一个数字:

'1'
'2'
'3'
'4'
'5'

你能帮我吗?

最佳答案

您可以使用 zip 将字母与数字分开,并使用 mapzip 返回的元组转换为列表:

array = [['a','1'], ['b','2'], ['c','3'], ['d','4'], ['e','5']]

letters, numbers = map(list, zip(*array))

print(letters)
print(numbers)

输出:

['a', 'b', 'c', 'd', 'e']
['1', '2', '3', '4', '5']

关于python - 如何拆分数组数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56104347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com