Python implements two list corresponding element subtraction examples

  • 2020-06-03 07:06:29
  • OfStack

This article is an example of how Python implements the subtraction of two corresponding list elements. To share for your reference, specific as follows:

Two corresponding element operations of list, take subtraction as an example:


# coding=gbk
v1 = [21, 34, 45]
v2 = [55, 25, 77]
#v = v2 - v1      # Error: TypeError: unsupported operand type(s) for -: 'list' and 'list'
v = list(map(lambda x: x[0]-x[1], zip(v2, v1)))
print("%s\n%s\n%s" %(v1, v2, v))

Operation results:


E:\Program\Python>del.py
[21, 34, 45]
[55, 25, 77]
[34, -9, 32]

More about Python related topics: interested readers to view this site "Python list (list) skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article has been helpful in Python programming.


Related articles: