Python Forum
NumPy Side Effects - HELP UNDERSTANDING
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NumPy Side Effects - HELP UNDERSTANDING
#1
I AM CURRENTLY ON CHAPTER 4 WITH DATACAMP. SO FAR, SO GOOD

HOWEVER, I AM TRYING TO UNDERSTAND THIS

Have a look at this line of code:

np.array([True, 1, 2]) + np.array([3, 4, False])
Can you tell which code chunk builds the exact same Python object? The numpy package is already imported as np, so you can start experimenting in the IPython

HOW DOES

np.array([True, 1, 2]) + np.array([3, 4, False])

=

np.array([4, 3, 0]) + np.array([0, 2, 2])

CAN SOMEONE EXPLAIN THIS IN SIMPLE TERMS PLEASE :-)
Reply
#2
Python treats True like 1, and False like 0. Other than that, it's just summing the respective elements. It's much like this:
Output:
>>> def sum_elements(first_elements, second_elements): ... return [x + y for x, y in zip(first_elements, second_elements)] ... >>> sum_elements([True, 1, 2], [3, 4, False]) [4, 5, 2] >>> sum_elements([4, 3, 0], [0, 2, 2]) [4, 5, 2]
Reply
#3
Also, please avoid writing in all caps, as it looks like you're shouting.
Reply
#4
(Sep-16-2020, 10:08 PM)micseydel Wrote: Python treats True like 1, and False like 0. Other than that, it's just summing the respective elements. It's much like this:
Output:
>>> def sum_elements(first_elements, second_elements): ... return [x + y for x, y in zip(first_elements, second_elements)] ... >>> sum_elements([True, 1, 2], [3, 4, False]) [4, 5, 2] >>> sum_elements([4, 3, 0], [0, 2, 2]) [4, 5, 2]

Ahhh okay. Thank you for explaining :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to horizontally align and display images side-by-side in an email using Python? shantanu97 0 1,007 Feb-22-2023, 11:41 PM
Last Post: shantanu97
  Get the Client-Side TLS ? JohnnyCoffee 1 1,354 Nov-30-2021, 10:49 PM
Last Post: Larz60+
  4D array with only elements on one side of the diagonal schniefen 0 1,693 Dec-24-2020, 11:32 AM
Last Post: schniefen
  Explanation of the left side of this statement please rascalsailor 3 2,510 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  putting 2 lists items side by side Jiwan 1 2,245 Apr-30-2020, 01:04 AM
Last Post: Larz60+
  How to print data in bracket in list in one side. phob0s 2 2,188 Jul-23-2019, 08:00 AM
Last Post: phob0s
  Publishing two boards side by side DJ_Qu 4 2,494 May-15-2019, 08:57 PM
Last Post: Yoriz
  Multivariate mixed effects models primeprover 1 2,595 Aug-19-2018, 01:14 PM
Last Post: ketanm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020