简体   繁体   English

如何有效地修改列表列表的所有元素并将其添加到现有列表列表中

[英]How to efficiently modify all elements of a list of lists and add them to the existing list of lists

Suppose I have a list of list that looks like this: 假设我有一个列表列表,看起来像这样:

myList = [[1.,1.,6.],[2.,4.,4.],[3.,3.,3.]]

Now I want to apply a certain function to each list in myList which - for the sake of simplicity - looks like this (it just divides each element of the list by the sum of this list). 现在,我想对myList中的每个列表应用某个函数,为简单起见,它看起来像这样(它只是将列表中的每个元素除以该列表的总和)。

def changeVal(l):
    return map(lambda x: x/sum(l),l)

Applying it to myList gives me: 将其应用于myList给我:

modList = map(lambda x: changeVal(x), myList)
[[0.125, 0.125, 0.75],
[0.2, 0.4, 0.4],
[0.3333333333333333, 0.3333333333333333, 0.3333333333333333]]

But what I actually want is to add all elements of modList to myList . 但是我真正想要的是将modList所有元素添加到myList I could do this in a for-loop: 我可以在for循环中执行此操作:

for sl in modList:
    myList.append(sl)

which gives me the desired output: 这给了我想要的输出:

[[1.0, 1.0, 6.0],
 [2.0, 4.0, 4.0],
 [3.0, 3.0, 3.0],
 [0.125, 0.125, 0.75],
 [0.2, 0.4, 0.4],
 [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]]

However, I want to do this without using a for-loop since append is slow. 但是,我想不使用for循环来执行此操作,因为append速度很慢。 I tried: 我试过了:

myList.append(*modList)
myList.extend(*modList)

which both give me an TypeError : 两者都给我一个TypeError

TypeError: extend() takes exactly one argument (3 given)

Here is the code again and the desired output: 这又是代码和所需的输出:

myList = [[1.,1.,6.],[2.,4.,4.],[3.,3.,3.]]
def changeVal(l):
    return map(lambda x: x/sum(l),l)
modList = map(lambda x: changeVal(x), myList)

How to add the elements of modList to myList in order to get the following output? 如何将modList的元素添加到myList以获得以下输出?

[[1.0, 1.0, 6.0],
 [2.0, 4.0, 4.0],
 [3.0, 3.0, 3.0],
 [0.125, 0.125, 0.75],
 [0.2, 0.4, 0.4],
 [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]]

The correct function to use here would be extend() , but extend accepts the list you want to extend with, not the elements of that list. 此处使用的正确函数应该是extend() ,但是extend接受您要扩展的列表,而不是该列表的元素。 Try something like - 尝试类似-

myList.extend(modList)

Example - 范例-

>>> l = [[1,2],[3,4]]
>>> l1 = [[5,6],[7,8]]
>>> l.extend(l1)
>>>
>>> l
[[1, 2], [3, 4], [5, 6], [7, 8]]

Though if modList is not used anywhere else, you can avoid creation of the intermediate list altogether, By doing - 尽管如果在其他任何地方都没有使用modList则可以通过以下方式完全避免创建中间列表:

myList.extend(map(lambda x: changeVal(x), myList))

Timeit results for both method - 两种方法的timeit结果-

In [42]: def func1():
   ....:     l = [[1,2],[3,4]]
   ....:     l1 = [[5,6],[7,8]]
   ....:     for i in l1:
   ....:         l.append(i)
   ....:     return l
   ....:

In [43]: def func2():
   ....:     l = [[1,2],[3,4]]
   ....:     l1 = [[5,6],[7,8]]
   ....:     l.extend(l1)
   ....:     return l
   ....:

In [44]:

In [44]: %timeit func1()
The slowest run took 8.35 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1 µs per loop

In [45]: %timeit func2()
The slowest run took 9.11 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 794 ns per loop

In [47]: %timeit func1()
The slowest run took 7.74 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 983 ns per loop

In [46]: %timeit func2()
1000000 loops, best of 3: 799 ns per loop

So using .extend() is a little bit faster than for loop and .append() . 因此,使用.extend()for loop和.append()快一点。

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

相关问题 如何通过添加列表来使列表列表中的所有列表具有相同的长度 - How to make all lists in a list of lists the same length by adding to them 创建列表列表。 修改列表中的元素 - Create a list of lists. Modify elements in the list 如何有效地获取 Python 中两个列表列表中元素的平均值 - How to efficiently get the mean of the elements in two list of lists in Python Python: Output 的元素列表作为单独的列表,如何将它们都放在一个唯一的列表中? - Python: Output of the elements of the list as separate lists, how to put them all in a unique list? 如何在列表列表中添加特定元素(即字符串)? - How to add specific elements (that are strings) in a List of Lists? 如何从现有列表列表中创建第一个元素的新列表 - How to make a new list of first elements from existing list of lists 如何从嵌套在特定元素之间的列表中提取元素,并将它们添加到新列表中? - How to pull elements from a list that are nested between particular elements, and add them to new lists? 如何修改列表中的值 - How to modify values in list of lists 如何在Python的列表列表中添加所有项目 - How to add all items in a list of lists in Python 我如何将2个排序后的列表添加到另一个列表中,同时删除元素 - How do I take 2 sorted lists and add them to another list while removing the elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM