【Python】列表解析表达式

1、语法

[expression for iter_val in iterable]

[expression for iter_val in iterable if cond_expr]

2、示例

1 s = "abcdefg"
2 #将字符串s拆分成列表
3 list_s = [i for i in s]
4 print(list_s)
1 s = "abcdefg"
2 s_2 = "cdefgkgz"
3 #求s_2和s的共有元素
4 list_s = [i for i in s if i in s_2]
5 print(list_s)

 

posted @ 2018-07-08 10:36  程小白  阅读(2016)  评论(0编辑  收藏  举报