Ask Your Question
0

How to define conditionaly two vector from one

asked 2020-06-08 00:20:04 +0100

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-08 00:22:49 +0100

tmonteil gravatar image

updated 2020-06-08 00:23:28 +0100

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 08:02:58 +0100 )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-08 00:20:04 +0100

Seen: 201 times

Last updated: Jun 08 '20