[英]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.