简体   繁体   English

从“列表”和“列表列表”中创建两个单独的“列表列表”

[英]Creating two separate “lists of lists” out of “list” & “list of lists”

I haven't found a similar question with a solution here, so I'll ask your help.我在这里没有找到类似的问题和解决方案,所以我会寻求你的帮助。

There are 2 lists, one of which is list of lists:有 2 个列表,其中之一是列表列表:

categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]

As a final result I would like to split it by 2 separate lists of lists:作为最终结果,我想将其拆分为 2 个单独的列表列表:

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

I tried to create loops but I have nothing to share.我试图创建循环,但我没有什么可分享的。 Any ideas are appreciated.任何想法表示赞赏。

This is a simple answer.这是一个简单的答案。 Hope its what you were looking for.希望它是你要找的。

categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]

# Declare 2 lists
list1 = []
list2 = []

for category, results in zip(categories, test_results):
    # Append to each list
    list1.append([category, results[0]])
    list2.append([category, results[1]])

# Print lists
print(list1)
print(list2)

You can do it simply using list traversal and choosing element at index from first list and values from second list:您可以简单地使用列表遍历并从第一个列表中选择索引处的元素和第二个列表中的值来做到这一点:

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']]

You can use some Python built-in functions.您可以使用一些 Python 内置函数。

for i in range(len(test_results[0])):
    print(list(map(list, zip(categories, map(lambda x: x[i], test_results)))))

list comprehension:列表理解:

categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]

total_list = [[[category, num] for category, num in zip(categories, i)] for i in zip(*test_results)]
l1, l2 = total_list # due to there are only two sub-list in total list.
print(l1, l2, total_list, sep="\n")

Another way of doing this:另一种方法:

from itertools import chain
test_results = list(chain.from_iterable(test_results))
l1, l2 = list(map(list,zip(categories, test_results[::2]))), list(map(list,zip(categories, test_results[1::2])))

output: output:

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

Another more generalizable version:另一个更通用的版本:

from itertools import chain, repeat
test_results = list(chain.from_iterable(test_results))
categories = list(chain.from_iterable(repeat(x,2) for x in categories))
l = list(map(list,zip(categories, test_results)))
l1, l2 = l[::2], l[1::2]
categories = ['APPLE', 'ORANGE', 'BANANA']
test_results = [['17.0', '12.0'], ['21.0', '15.0'], ['7.0', '6.0']]
l = []
d=0
for i in range(len(categories)):
    k = len(test_results[0])
    for j in range(len(categories)):
        l.append([categories[j],test_results[j][d]])
    print(l)
    l=[]
    d+=1
    if d==k:
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM