python将3个列表合并到1个列表中(python merge 3 lists into 1 list)
我想将3个列表合并到Python中的单个列表中。 例如,我有三个列表:
a = [0, 3, 6, 9] b = [1, 4, 7, 10] c = [2, 5, 8, 11]
最后想要得到
merged = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
出于a,b,c
有没有更快的方法来合并这3个列表? 这是我的代码
merged=[] a = [0, 3, 6, 9] b = [1, 4, 7, 10] c = [2, 5, 8, 11] for i in range(0, len(a)) : merged.append(a[i]) merged.append(b[i]) merged.append(c[i])
I want to merge 3 lists in to a single list in python. For example, i have three lists looks :
a = [0, 3, 6, 9] b = [1, 4, 7, 10] c = [2, 5, 8, 11]
and finally want to get
merged = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
out of a, b, c
are there any faster way to merge these 3 lists? here's my code
merged=[] a = [0, 3, 6, 9] b = [1, 4, 7, 10] c = [2, 5, 8, 11] for i in range(0, len(a)) : merged.append(a[i]) merged.append(b[i]) merged.append(c[i])
原文:https://stackoverflow.com/questions/34761978
最满意答案
简短而有效的解决方案:
def splitList (lst, n): it = iter(lst) new = [[next(it) for _ in range(n)] for _ in range(len(lst) // n)] for i, x in enumerate(it): new[i].append(x) return new
>>> lst = ['milk', 'eggs', 'beef', 'oranges', 'dog food', 'chips', 'soda', 'bread'] >>> splitList(lst, 3) [['milk', 'eggs', 'beef', 'soda'], ['oranges', 'dog food', 'chips', 'bread']] >>> splitList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3) [[1, 2, 3, 10], [4, 5, 6], [7, 8, 9]]
首先创建一个我们用于一切的迭代器; 所以总的来说,我们只在列表中循环一次。 将创建的子列表的数量是
len(lst) // n
(整数除法舍入),并且对于每个子列表,我们从迭代器中获取n
值。之后,剩余的项目仍然会留在迭代器中,所以我们可以简单地迭代其余的项目并直接将它们附加到子列表中。
A short but efficient solution:
def splitList (lst, n): it = iter(lst) new = [[next(it) for _ in range(n)] for _ in range(len(lst) // n)] for i, x in enumerate(it): new[i].append(x) return new
>>> lst = ['milk', 'eggs', 'beef', 'oranges', 'dog food', 'chips', 'soda', 'bread'] >>> splitList(lst, 3) [['milk', 'eggs', 'beef', 'soda'], ['oranges', 'dog food', 'chips', 'bread']] >>> splitList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3) [[1, 2, 3, 10], [4, 5, 6], [7, 8, 9]]
This first creates a single iterator which we use for everything; so in total we only loop once over the list. The number of sublists that will be created is
len(lst) // n
(integer division rounds down), and for each sublist we taken
values from the iterator.After that, the remaining items will be still left in the iterator, so we can simply iterate over the rest of them and append them to the sublists directly.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
要同时循环两个列表: public bool equalsSubList(CharNode other) { CharNode node1 = this; CharNode node2 = other; while (node1 != null && node2 != null) { // compare data from both nodes node1 = node1.getNext(); node2 = node2.getNext(); } // ...
-
如果您的输入是具有快速随机访问权限的List ,则可以使用索引流来解决您的问题: public static List
f(int n, List input) { int fence = IntStream.range(0, input.size()) .filter(idx -> input.get(idx).equals("B")) // leave only B's ... -
如果子列表是一定长度,如何将项目插入子列表?(How to insert an item into a sublist if sublist is a certain length?)[2021-12-08]
如果你已经改变了,请考虑使用它而不是创建一个新的列表Desired_List ,只需: >>> for sublist in lst: ... if len(sublist) == 3: ... sublist.insert(2,"Null") ... >>> lst [['4', '4', '4', '4'], ['3', '3', 'Null', '3'], ['1', '42', '', '4'], ['1', '2', 'Null', '3']] if you already ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
scala> def round[T](l: List[T], n: Int) = (0 until n).map{ i => l.drop(i).sliding(1, n).flatten.toList }.toList round: [T](l: List[T], n: Int)List[List[T]] scala> round((1 to 7).toList, 3) res4: List[List[Int]] = List(List(1, 4, 7), List(2, 5), List(3, 6) ...
-
如何使用subList()(How to use subList())[2023-06-05]
使用子subList(30, 38); 将失败,因为最大索引38在列表中不可用,所以它是不可能的。 只有在查询子列表之前,您可以使用list size()方法明确确定最大索引。 例如,检查大小,返回35,所以调用sublist(30, size()); 要么 COPIED FROM pb2q comment dataList = dataList.subList(30, 38 > dataList.size() ? dataList.size() : 38); Using subList(30, 38); ... -
您可以使用Record.getSublists()检索子列表ID并记录它以确保您拥有有效的子列表ID。 我怀疑这可能与SuiteScript 2.0错误有关,所以请检查NetSuite并尝试在SuiteScript 1.0中重写它。 You can retrieve the sublist ids using Record.getSublists() and log that to make sure you have a valid sublist ID. I suspect this could be ...
-
将列表拆分成子列表中的n个项目,并添加其余部分[closed](Split a list into n items in a sublist with remainder being added [closed])[2022-07-16]
简短而有效的解决方案: def splitList (lst, n): it = iter(lst) new = [[next(it) for _ in range(n)] for _ in range(len(lst) // n)] for i, x in enumerate(it): new[i].append(x) return new >>> lst = ['milk', 'eggs', 'beef', 'oranges', 'dog food' ... -
如何过滤项目的子列表(How to filter a sublist of items)[2022-05-12]
尝试这个: var result = from container in containers.Where(c => c.Active) from item in container.Items where item > 2 select container; 标准格式: var standard_result = containers .Where(container => container.Active && co ...