Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Mod Removes Wiki by Doorknob
deleted 7 characters in body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676

Adding vectors

Python doesn't have a built-in way to do vector (component-wise) addition excepyexcept with libraries. Say a and b are two equal-length lists of numbers you want to add. Instead of the list comprehension

c=[a[i]+b[i]for i in range(len(a))]

you can use

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even if you have to convert to a list.

Adding vectors

Python doesn't have a built-in way to do vector (component-wise) addition excepy with libraries. Say a and b are two equal-length lists of numbers you want to add. Instead of the list comprehension

c=[a[i]+b[i]for i in range(len(a))]

you can use

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even if you have to convert to a list.

Adding vectors

Python doesn't have a built-in way to do vector (component-wise) addition except with libraries. Say a and b are two equal-length lists of numbers you want to add. Instead of the list comprehension

c=[a[i]+b[i]for i in range(len(a))]

you can use

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3, but it's shorter even if you have to convert to a list.

edited body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676

Adding vectors

Adding vectors

Python doesn't have a built-in way to do vector (component-wise addition) withoutaddition excepy with libraries. Say a and b are two equal-length lists of numbers you want to add.

  Instead of the list comprehension,

c=[a[i]+b[i]for i in range(len(a))]

you can douse

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even with converting itif you have to convert to a list.

Adding vectors

Python doesn't have a built-in way to do vector (component-wise addition) without libraries. Say a and b are two equal-length lists of numbers.

  Instead of the list comprehension,

c=[a[i]+b[i]for i in range(len(a))]

you can do

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even with converting it to a list.

Adding vectors

Python doesn't have a built-in way to do vector (component-wise) addition excepy with libraries. Say a and b are two equal-length lists of numbers you want to add. Instead of the list comprehension

c=[a[i]+b[i]for i in range(len(a))]

you can use

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even if you have to convert to a list.

Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676

Adding vectors

Python doesn't have a built-in way to do vector (component-wise addition) without libraries. Say a and b are two equal-length lists of numbers.

Instead of the list comprehension,

c=[a[i]+b[i]for i in range(len(a))]

you can do

c=map(sum,zip(a,b))

This produces an annoying map object in Python 3 though, but it's shorter even with converting it to a list.