Python Forum
if and for statements on the same line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if and for statements on the same line
#1
Hello friends,
I have a very easy question but since I am a beginner I could not solve it.
I have a list like
a=[1 2 3 -4]
what I wanted to do
if (condition x>condition y and all the elements of list a is greater than 0)
do this
else:
do that
how can I check the each element of list a if they are greater than 0 on the "if" line?
Thank you so much
I tried to to do something like if (condition x>condition y and [n for n in a >0]:
but it did not work
I will be very happy, if you can help me
#2
if conditionx > conditiony and all(n > 0 for n in a):

or

if conditionx > conditiony and all(map(lambda x: x >0, a)):

the first one is better
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

#3
Thank you so much buran , it worked :)
#4
Hello Buran ,
I have another similar question . This time I have an matrix like
C=[[1 2 3]
[4 5 6]
[7 8 9]
I need to choose second line [4 5 6] and check if each element is greater than 10. (check 4>10 ,5>10,6>10) and I have to write it in rhe same same with the previous idea . Let's say
if a>b and first element of the second line is greater than 10. (until third element of second line )
How can I write it ?
Thank you so much
#5
(Sep-17-2018, 08:17 AM)juniorcoder Wrote: Hello Buran ,
I have another similar question . This time I have an matrix like
C=[[1 2 3]
[4 5 6]
[7 8 9]
I need to choose second line [4 5 6] and check if each element is greater than 10. (check 4>10 ,5>10,6>10) and I have to write it in rhe same same with the previous idea . Let's say
if a>b and first element of the second line is greater than 10. (until third element of second line )
How can I write it ?
Thank you so much
Asked in three places!
I am trying to help you, really, even if it doesn't always seem that way


Possibly Related Threads…
Thread Author Replies Views Last Post
  If-Print statements on the same line ClassicalSoul 1 2,268 Jan-31-2019, 02:14 PM
Last Post: buran
  Do I always have to use triple quotes or \n for multi-line statements? DragonG 3 2,624 Oct-24-2018, 11:21 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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