Ask Your Question
0

How to define conditionaly two vector from one

asked 2020-06-07 17:20:04 -0500

Cyrille gravatar image

I want to construct two vectors from one according to a condition.

zouzou=[random() for _ in range(0,10)]
len(zouzou)
zouzou
def conds(x,a) :
    if x > a : 
        return x
    else :  
        return 0
def condi(x,a) :
    if x <= a : 
        return x
    else :  
        return 0

when I try

show(conds(zouzou[2],.5))
show(condi(zouzou[3],.5))

I have no problems but when I use one or the other function to create a vector as in

zouzous=[conds(zouzou[_],.5) if _ in range(0,10)]

Sage return a syntax error.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-06-07 17:22:49 -0500

tmonteil gravatar image

updated 2020-06-07 17:23:28 -0500

You should replace:

[conds(zouzou[_],.5) if _ in range(0,10)]

with

[conds(zouzou[_],.5) for _ in range(0,10)]
edit flag offensive delete link more

Comments

Thanks sorry to be so long to understand how to program. I have also found that

zouzous=[conds(x,.5) for x in zouzou] works.

Cyrille gravatar imageCyrille ( 2020-06-08 01:02:58 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-06-07 17:20:04 -0500

Seen: 38 times

Last updated: Jun 07