我有一个数组,其中每个值都类似于 (5 , "d"),带有一个整数,然后是一个字符串。我有很多这样的数组。如何将每组这些拆分为整数和字符串,如 5 和“d”?
尝试使用zip
,如评论中所建议:
>>> myarr = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
>>> ints, strs = zip(*myarr)
>>> ints
(1, 2, 3, 4, 5)
>>> strs
('a', 'b', 'c', 'd', 'e')
>>> list(ints)
[1, 2, 3, 4, 5]
>>> list(strs)
['a', 'b', 'c', 'd', 'e']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句