Python Forum
Formula with elements of list - If-condition regarding the lists elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formula with elements of list - If-condition regarding the lists elements
#1
Hi guys,

I am a beginner with Python so please don´t be too harsh on me.

Currently I am working on recursive functions in general.

Please take a look at my code, which contains just a simple formula y = x1 + x2, where x1 should be an element of my list "lst1".

import numpy as np
lst1 = [1.3, 2.4, 3.7, 4.4, 5.5, 5.8]
x1 = np.array(lst1)
if x1.any() == 1.3:
    x2 = 3
else:
    x2 = 10
y = x1 + x2
print y
This simple code is for me to test out the mechanism of if-conditions which result in different values for a variable (in my case x2).

For me, it looks like the code says in line 4:

If any value of my lst1 is 1.3, then x2 = 3. So y would be printed out as 4.3 for the first element of my list.

But my code does not do that. The y for the first element is printed out as 11.3, so Python doesn´t see the if condition as given.

Why is that?

My goal is to be able to set up a bunch of if conditions, which are respected for each element one after another as Python goes trough my lists elements.

Thank you very much for your patience.

Greetings

lewie
Reply
#2
Try this code:

if lst1.any(x1):

Actually, I just read an article on any(). It returns based on the boolean value of an item in an iterable.

Examples:
list = [0, False]
print(any(list))
This will return False because 0 and False = False.
Reply
#3
Hope below code clarifies your doubt.
import numpy as np
lst1 = [1.3, 2.4, 3.7, 4.4, 5.5, 5.8]
x1 = np.array(lst1)
x2+3 if 1.3 in lst1 else x2+10
y = x1 + x2
print(y)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OR-Tools LP Limit Number of Elements in Solution idratherbecoding 0 743 May-14-2023, 11:02 PM
Last Post: idratherbecoding
  Turn list of arrays into an array of lists Cola_Reb 6 1,663 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
  Printing file path of lift elements dyerlee91 1 1,477 Sep-27-2021, 01:22 PM
Last Post: snippsat
  Join each list elements with string in other DF handy88 0 1,519 Feb-09-2021, 07:00 PM
Last Post: handy88
  updating cluster of elements based on the max value of distance alex80 0 1,575 Oct-02-2020, 11:11 AM
Last Post: alex80
  Fastest way to subtract elements of datasets of HDF5 file? Robotguy 3 2,592 Aug-01-2020, 11:48 PM
Last Post: scidam
  define certain array elements+display with digits lukezo 0 1,232 Apr-10-2020, 05:03 PM
Last Post: lukezo
  How to prepare a NumPy array which include float type array elements subhash 0 1,885 Mar-02-2020, 06:46 AM
Last Post: subhash
  How to access dataframe elements SriMekala 4 3,177 Jul-30-2019, 01:50 AM
Last Post: scidam
  When dividing elements of matrix i would like the result 0 instead of inf? Koczkodan 4 2,870 Jul-22-2019, 11:40 AM
Last Post: Koczkodan

Forum Jump:

User Panel Messages

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