Python中的comprehension syntax, conditional expression与 ternary operator

comprehension syntax 有俩种list, dict


conditional expression:

if 在for后面, if修饰整个语句

never_login_users = [user for user in new_shared_user_ids if is_user_never_login(user)]

ternary operator:

if 在 for前面, 只修饰 最前面的user

never_login_users = [user if is_user_never_login(user) else '' for user  in new_shared_user_ids]
 

你可能感兴趣的:(Python中的comprehension syntax, conditional expression与 ternary operator)