#一般形式 mylist_one=[x for x in range(1,100,2)]#输出1-100的奇数 mylist_two=[x+1 for x in range(1,100,2)]#输出1-100的偶数 print(mylist_one,'mylist_one');print(mylist_two,'mylist_two') # #一般形式+判断 mylist_one=[x for x in range(1,100,2) if x >50]#输出50之后的奇数 mylist_two=[x+1 for x in range(1,100,2) if x >50]#输出50之后的偶数 print(mylist_one,'mylist_one');print(mylist_two,'mylist_two') #列表嵌套列表 mylist_one=[[x,x+1,x*x] for x in range(1,100,2)]#输出50之后的奇数 print(mylist_one,'mylist_one') #无限循环嵌套 mylist_one=[[x,y] for x in range(10) for y in range(10)]#输出50之后的奇数 print(mylist_one,'mylist_one')
输出结果:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] mylist_one [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] mylist_two [51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] mylist_one [52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] mylist_two [[1, 2, 1], [3, 4, 9], [5, 6, 25], [7, 8, 49], [9, 10, 81], [11, 12, 121], [13, 14, 169], [15, 16, 225], [17, 18, 289], [19, 20, 361], [21, 22, 441], [23, 24, 529], [25, 26, 625], [27, 28, 729], [29, 30, 841], [31, 32, 961], [33, 34, 1089], [35, 36, 1225], [37, 38, 1369], [39, 40, 1521], [41, 42, 1681], [43, 44, 1849], [45, 46, 2025], [47, 48, 2209], [49, 50, 2401], [51, 52, 2601], [53, 54, 2809], [55, 56, 3025], [57, 58, 3249], [59, 60, 3481], [61, 62, 3721], [63, 64, 3969], [65, 66, 4225], [67, 68, 4489], [69, 70, 4761], [71, 72, 5041], [73, 74, 5329], [75, 76, 5625], [77, 78, 5929], [79, 80, 6241], [81, 82, 6561], [83, 84, 6889], [85, 86, 7225], [87, 88, 7569], [89, 90, 7921], [91, 92, 8281], [93, 94, 8649], [95, 96, 9025], [97, 98, 9409], [99, 100, 9801]] mylist_one [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7], [4, 8], [4, 9], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7], [5, 8], [5, 9], [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [6, 8], [6, 9], [7, 0], [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6], [7, 7], [7, 8], [7, 9], [8, 0], [8, 1], [8, 2], [8, 3], [8, 4], [8, 5], [8, 6], [8, 7], [8, 8], [8, 9], [9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 9]] mylist_one