Blogger Information
Blog 41
fans 0
comment 1
visits 40513
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Python高效编程技巧实战(6)
yeyiluLAMP
Original
779 people have browsed it

snipaste20170917_135929.png

from random import randint
from itertools import chain


#并行 

Chinese = [randint(40,100) for x in range(10)]
Math = [randint(50,100) for x in range(10)]
English = [randint(60,100) for x in range(10)]

方法一:
total = []
for c,m,e in zip(Chinese,Math,English):
    total.append(c + m + e)
print(total)

方法二:
[c + m + e for c,m,e in zip(chinese,math,english)]


#串行 

In [123]: e1 = [randint(60,100) for x in range(40)]

In [124]: e2 = [randint(60,100) for x in range(42)]

In [125]: e3 = [randint(60,100) for x in range(42)]

In [126]: e4 = [randint(60,100) for x in range(39)]

方法一:
In [127]: count = 0

In [128]: for x in chain(e1,e2,e3,e4):
   .....:     if x >= 90:
   .....:         count += 1
   .....:         

In [129]: count
Out[129]: 48

方法二(推荐):

In [130]: len([x for x in chain(e1,e2,e3,e4) if x >= 90])
Out[130]: 48

方法三:
In [134]: len(list(filter(lambda x:x >= 90,chain(e1,e2,e3,e4))))
Out[134]: 48


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post