在没有numpy的列表上进行元素明智操作的正确样式(python) [英] correct style for element-wise operations on lists without numpy (python)
问题描述
我想在不使用numpy的情况下逐个元素地操作列表,例如,我想要add([1,2,3], [2,3,4]) = [3,5,7]
和mult([1,1,1],[9,9,9]) = [9,9,9]
,但是我不确定哪种方法被认为是正确的"样式.
I would like to operate on lists element by element without using numpy, for example, i want add([1,2,3], [2,3,4]) = [3,5,7]
and mult([1,1,1],[9,9,9]) = [9,9,9]
, but i'm not sure which way of doing is it considered 'correct' style.
我想出的两个解决方案是
The two solutions i came up with were
def add(list1,list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]+list2[x])
return list3
def mult(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]*list2[x])
return list3
def div(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]/list2[x])
return list3
def sub(list1, list2):
list3 = []
for x in xrange(0,len(list1)):
list3.append(list1[x]-list2[x])
return list3
为每个运算符赋予一个单独的功能
where each operator is given a separate function
和
def add(a,b)
return a+b
def mult(a,b)
return a*b
def div(a,b)
return a/b
def sub(a,b)
return a-b
def elementwiseoperation(list1, list2, function):
list3 = []
for x in xrange(0,len(list1)):
list3.append(function(list1[x],list2[x]))
return list3
其中定义了所有基本功能,并且我有一个单独的函数可在每个元素上使用它们.我浏览了PEP8,但没有发现任何直接相关的内容.哪种方法更好?
where all the basic functions are defined, and I have a separate function to use them on each element. I skimmed through PEP8, but didn't find anything directly relevant. Which way is better?
推荐答案
通常的方法是使用map
或itertools.imap
:
import operator
multiadd = lambda a,b: map(operator.add, a,b)
print multiadd([1,2,3], [2,3,4]) #=> [3, 5, 7]
Ideone: http://ideone.com/yRLHxW
map
是elementwiseoperation
的c实现版本,具有标准名称,使用任何可迭代类型且速度更快的优点(在某些版本上;有关某些配置文件,请参阅@nathan的回答).
map
is a c-implemented version of your elementwiseoperation
, with the advantage of having the standard name, working with any iterable type and being faster (on some versions; see @nathan's answer for some profiling).
或者,您可以将partial
和map
用作令人愉悦的无点样式:
Alternatively, you could use partial
and map
for a pleasingly pointfree style:
import operator
import functools
multiadd = functools.partial(map, operator.add)
print multiadd([1,2,3], [2,3,4]) #=> [3, 5, 7]
Ideone: http://ideone.com/BUhRCW
无论如何,您已经亲自完成了函数式编程的第一步.我建议您阅读该主题.
Anyway, you've taken the first steps in functional programming yourself. I suggest you read around the topic.
一般来说,如果要访问每个项目,使用range
通过索引进行迭代通常被认为是错误的事情.这样做的通常方法是直接对结构进行直接迭代.使用zip
或itertools.izip
进行并行迭代:
As a general matter of style, iterating by index using range
is generally considered the wrong thing, if you want to visit every item. The usual way of doing this is simply to iterate the structure directly. Use zip
or itertools.izip
to iterate in parallel:
for x in l:
print l
for a,b in zip(l,k):
print a+b
迭代创建列表的通常方法不是使用append
,而是使用列表理解:
And the usual way to iterate to create a list is not to use append
, but a list comprehension:
[a+b for a,b in itertools.izip(l,k)]
这篇关于在没有numpy的列表上进行元素明智操作的正确样式(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!