列表解析

 1 #!/usr/bin/env python
 2 l1 = ['x','y','z','w','e','r','t','v',]
 3 l2 = [1,3,4,5,2,6,7,]
 4 if len(l1) <= len(l2):
 5     for m in range(len(l1)):
 6         for i in l1:
 7             for j in l2:
 8                 if l1[m] == i and l2[m] == j:
 9                     print((i,j))
10 else:
11     for m in range(len(l2)):
12         for i in l1:
13             for j in l2:
14                 if l1[m] == i and l2[m] == j:
15                     print((i,j))
16 
17 print('\n以下是列表解析')
18 if len(l1) <= len(l2):
19     l3 = [(i,j) for i in l1 for j in l2 for m in range(len(l1)) if l1[m] == i and l2[m] == j]      #列表解析 
20     for (i,j) in l3:
21         print((i,j))
22 else:
23     l3 = [(i,j) for i in l1 for j in l2 for m in range(len(l2)) if l1[m] == i and l2[m] == j]      #列表解析 
24 for (i,j) in l3: 25 print((i,j)) 26 27 print('\n看一看') 28 print('l3 = %s'% l3) 29 s3 = dict(l3) 30 print('s3 = %s'% s3) 31 print(type(s3))

效果:

posted @ 2018-05-29 22:32  头发乱刘  阅读(181)  评论(0编辑  收藏  举报