python - 如何以最 pythonic 的方式使用 Python 转换元组列表(可能使用 zip)

标签 python list tuples

我有这个数据结构:

[((u'die', u'die'), (u'welt', u'WELT')), 
 ((u'welt', u'WELT'), (u'ist', u'ist'))]

如何以最pythonic的方式将上面的结构转换为下面的结构?带 zipper ?

[((u'die', u'welt'), (u'die', u'WELT')), 
 ((u'welt', u'ist'), (u'WELT', u'ist'))]

最佳答案

您可以使用 zip() 函数并对此进行列表推导,如 -

>>> lst = [((u'die', u'die'), (u'welt', u'WELT')),
...  ((u'welt', u'WELT'), (u'ist', u'ist'))]
>>>
>>> [tuple(zip(*x)) for x in lst]
[(('die', 'welt'), ('die', 'WELT')), (('welt', 'ist'), ('WELT', 'ist'))]

关于python - 如何以最 pythonic 的方式使用 Python 转换元组列表(可能使用 zip),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517572/

相关文章:

list - 在列表 monad 中使用 return 与不使用 return

c++ - 我怎样才能最好地向听众解释元组?

python - 如何将 numpy 元组数组乘以标量数组

c# - 从列表中获取对象名称

python - (重新)在 Django View 中使用字典

python - 棘手的模型继承 - Django

python - 随机将字符串中的字母大写

python - scrapy:错误:异常(exception)。AttributeError: 'Response'对象没有属性 'xpath'

python数组符号简写:"k%parents.shape[0]"是什么意思?

python - 如何工作 "expand cells containing lists into their own variables in pandas"