[Python]List Comprehension

 

students = [("jerry", 25), ("elaine", 24), ("John", 34), ("kramer", 34)]
#把年龄在30以上的学生信息提取出来
print([item for item in students if item[1] > 30])

#把年龄在30以上的学生姓名提取出来
print([item[0] for item in students if item[1] > 30])

#把名字以j(不论大小写)开头的学生信息提取出来
print([item for item in students if item[0].lower().startswith('j')])

 

posted @ 2020-06-18 20:49  profesor  阅读(144)  评论(0编辑  收藏  举报