首页 > 编程知识 正文

Python跳过,python unique函数

时间:2023-05-05 16:04:41 阅读:41914 作者:2992

ineedtomakeaprograminpythonthatgeneratestenrandomnumbersfrom1- 100 thatstoresitinalistusingaloop.thenasecondlooopshouldispla thencalculatethesumsoftheevenandoddelementstodisplaythem.thisiswhatihavesofar,anyhelpisgreatlyappreciated.thatihavesofar

导入随机

defmain(: )

forrinrange(10 ) :

是打印(numlist )

list_length=len(numlist )

打印(thenumberofelementsinthelistis ),list_length ) ) )。

morespecificallythisistheparti ' mstuckon.ihavetoaddthesumsoftheoddandthenelements.everyworkaroundi ' vetryedhasonlygily

forxinrange (0,10,2 ) :

打印(thesumoftheoddnumbersis (,sum ) (numlist ) ) ) ) )。

主() )

nums=[ random.randint (1,100 ) for_inrange ) 10]

You can use lambdas and filter

evensum=sum (过滤器(lambdai : I %2==0,nums ) )

od sum=sum (过滤器(lambdai : I % 2,nums ) )

Or make some quick helper functions

EFisEven(x ) :

返回x %2==0

EFisodd(x ) :

返回x %2==1

evensum=sum (过滤器(is even,nums ) )

od sum=sum (过滤器(is odd,nums ) )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。