if len(mylist): # Do something with my list else: # The list is empty
if mylist: # Do something with my list else: # The list is empty
i = 0 for element in mylist: # Do something with i and element i += 1
for i, element in enumerate(mylist): # Do something with i and element pass
class Person(object):
def __init__(self, age):
self.age = age
persons = [Person(age) for age in (14, 78, 42)]
def get_sort_key(element): return element.age for element in sorted(persons, key=get_sort_key): print "Age:", element.age
from operator import attrgetter
for element in sorted(persons, key=attrgetter('age')):
print "Age:", element.age
>>> squares = [x**2 for x in range(1,10)] >>> squares [1, 4, 9, 16, 25, 36, 49, 64, 81]
aliquot = []
for n in range(1,100):
if n%3 == 0:
aliquot.append(n)
print aliquot
>>> aliquot = [n for n in range(1,100) if n%3==0] >>> aliquot [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
>>> mybag = [' glass',' apple','green leaf '] #有的前面有空格,有的后面有空格 >>> [one.strip() for one in mybag] #去掉元素前后的空格 ['glass', 'apple', 'green leaf']
>>> for i in range(len(week)): ... print week[i]+' is '+str(i) #注意,i是int类型,如果和前面的用+连接,必须是str类型 ... monday is 0 sunday is 1 friday is 2
>>> for (i,day) in enumerate(week): ... print day+' is '+str(i) ... monday is 0 sunday is 1 friday is 2
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有