python : Splitting multidimensional lists

标签 python list unicode split

我有一个列表

[(u'a1', u'b1'),
 (u'a1', u'b2'),
 (u'c1', u'c2')]

我想把它分成两个列表/列,比如

list1          list2    
[(u'a1',       [(u'b1'),
 (u'a1',       (u'b2'),
 (u'c1')]       (u'c2')]

unicode 转换为字符串 也有帮助!

此外,在另一种情况下,我有以下形式的列表

[(('a', 'c'), -3), (('a', 'd'), -7), (('c', 'd'), -4)]

我需要以

的形式输入
('a','a','c')
('c','d','d')
(-3,-7,-4)

有什么建议吗?

最佳答案

您可以使用列表理解创建两个新列表:

x=[(u'a1', u'b1'),
 (u'a1', u'b2'),
 (u'c1', u'c2')]

list1 = [i[0] for i in x]

list2 = [i[1] for i in x]

关于 python : Splitting multidimensional lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15325773/

相关文章:

java - 在 Java 中将多个列表中的数据提取到新列表中的最优雅的方法是什么?

python - Unicode解码错误: 'ascii' codec can't decode byte 0xaa in position 2370: ordinal not in range(128)

c# - Azure PDF Sharp 不使用 Unicode 字体

python - Networkx:将 MultiDiGraph 转换为 DiGraph

python - Gunicorn gevent workers vs Uvicorn ASGI

c# - 检查列表中是否存在具有特定值的对象

python - Python 中的正则表达式和 Unicode : difference between sub and findall

python - 将从 keras.backend.argmax 返回的张量作为索引传递给 keras.backend,gather 预计为 'An integer tensor of indices.'

python Pandas : Is it possible to convert date Object to DateTimeIndex in multi-index dataframe?

Java 8 : Lambda expression contains more than one statement/logic