-1

Let's say I have 2 vectors [2, 4, 6, 8] and [1, 3, 5].

If I combine the numbers according to their indexes, I expect [3, 7, 11] as my result. If one vector has more indexes than the other, then it will be excluded in the calculation. (That's why 8 is not included here). My question is how do I combine 2 vectors while ignoring the extra 8 as shown above? I need a function.

def v_add(num1, num2):
    total = num1 + num2
    return total
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
user2581724
  • 237
  • 3
  • 9
  • 20

3 Answers3

5

You don't have vectors, but lists.

def v_add(num1, num2):
    return [a+b for a,b in zip(num1, num2)]
Daniel
  • 42,087
  • 4
  • 55
  • 81
  • It works but imo isn't as pythonic as alfasin's answer. – maxywb May 07 '14 at 17:16
  • 1
    @maxywb Most people find `map` unpythonic, and this solution will work as is in both Python 2 and 3. – Ashwini Chaudhary May 07 '14 at 17:20
  • 1
    I wouldn't go so far as to say `map()` is *unpythonic* - in general, list comprehensions/generator expressions are more readable, but in this case they are both very readable. I would say they are both completely pythonic. I'd probably go with `map()` here due to the simplicity of the case and the fact it makes the solution more generic (obviously, `[sum(numbers) for numbers in zip(num1, num2)]` would be as generic in a list comp, but it's more verbose). That being said, I wouldn't say there is much in it. – Gareth Latty May 07 '14 at 17:28
3
a = [1, 2, 3]
b = [1, 2, 3, 4]
c = map(sum, zip(a, b))
print(c)

OUTPUT

[2, 4, 6]
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Thank You. One more question, how would I Multiply the vectors? – user2581724 May 07 '14 at 17:31
  • 1
    @user2581724 If you have another question, ask another question - don't tack it on here - that way you will get better answers and others will be able to find it if they have the same question (in this case, do a search first - I imagine your question has been asked before). I'd also note that these are *lists*, not vectors in Python - using the correct terminology will make your question clearer and finding answers easier. – Gareth Latty May 07 '14 at 17:37
0
a = [1,2,3]
b = [4,5,6]
print [a[i] + b[i] for i in range(min(len(a),len(b)))]
  • 2
    -1, iterating by index in Python is simply a bad idea - it performs badly, reads badly, and there are better ways. – Gareth Latty May 07 '14 at 17:25
  • you anyway need to iterate through the lists,I don't think there can be algorithm better than O(n). – user1396267 May 07 '14 at 17:27
  • 1
    It's not about complexity, it's about the method of iteration. Python has loops that work over iterators (commonly 'for each' in other languages) - using them to iterate over numbers just to emulate the original behaviour is reinventing the wheel. It just makes code less readable and slower (not in complexity, just performance - unlikely to matter, but still). If nothing else, it makes your solution rely on the inputs being sequences rather than arbitrary iterables. – Gareth Latty May 07 '14 at 17:29
  • thank you Lattyware, I never had been comfortable with iterators – user1396267 May 07 '14 at 17:34
  • They are very core to Python - the entire standard library uses them extensively, and language features are built around them (e.g: generators, the for loop). They are actually a very simple (yet powerful) concept - worth reading up on if you are working in Python. – Gareth Latty May 07 '14 at 17:36