python - 从 "lists of lists"和 "list"创建两个单独的 "list of lists"

标签 python python-3.x list

我还没有在这里找到类似问题的解决方案,所以我会寻求你的帮助。
有 2 个列表,其中之一是列表列表:

categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]
作为最终结果,我想将它分成 2 个单独的列表列表:
[['APPLE','17.0']['ORANGE','21.0']['BANANA','7.0']]
[['APPLE','12.0']['ORANGE','15.0']['BANANA','6.0']]
我试图创建循环,但我没有什么可分享的。
任何想法表示赞赏。

最佳答案

您可以简单地使用 列表遍历 并从第一个列表中选择索引处的元素并从第二个列表中选择值:

l1 = []
l2 = []
for i, values in enumerate(test_results):
    l1.append([categories[i], values[0]])
    l2.append([categories[i], values[1]])

print(l1)
print(l2)

# Output
# [['APPLE', '17.0'], ['ORANGE', '21.0'], ['BANANA', '7.0']]
# [['APPLE', '12.0'], ['ORANGE', '15.0'], ['BANANA', '6.0']]

关于python - 从 "lists of lists"和 "list"创建两个单独的 "list of lists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63404909/

相关文章:

java - 如何使用 N - 1 个相等对象 + 1 个不同对象创建 List<T>?

python - 将集合转换为列表时出现参数错误

python - H2O服务器错误 : Cluster reports unhealthy status

python - Pyspark:将参数传递给数据帧中的字符串列

python - UnicodeDecodeError :'gbk' 编解码器无法解码位置 0 非法多字节序列中的字节 0x80

python - Mysql Python eof 然后在获取更多数据后返回

python - 导入错误 : No module named pip when trying to install packages

python - 如何在 Windows 8 中打开 PYW 文件

python - 具有固定元素的交错列表

python - 尝试设置 pandas 列名称时出现 "Bus error: 10"