Python Code:列表操作的一个样例
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#encoding=utf-8 __author__ = 'adamhuan' print "######################" print "Build List" print "######################" # Define List list_a = [] list_b = [] # Define Function def create_list_by_input(): user_input = raw_input("Input Your Data(列表元素以‘,’分隔):") return user_input.split(',') def print_data(some_data): print "-------------" print "The data is:",some_data print "-------------" # Running mask_str = raw_input("Input Your Mask String:\n") print "=== === ===" print "List One" list_a = create_list_by_input() print_data(list_a) print "=== === ===" print "List Two" list_b = create_list_by_input() print_data(list_b) print "=== === ===" print "Size of Target One:",len(list_a) print "Size of Target Two:",len(list_b) distance_count = abs(len(list_a) - len(list_b)) loop_count = 0 list_tuple = [] if (distance_count == 0): print "差额为:0,.. 不需要补全。" loop_count = len(list_a) print "循环上限:",loop_count for i in xrange(loop_count): list_tuple.append((list_a[i],list_b[i])) else: print "差额不为:0,.. 需要用MASK补全。" if(len(list_a) > len(list_b)): print "Bigger:Target One." loop_count = len(list_a) print "循环上限:",loop_count for i in xrange(loop_count): if (i < len(list_b)): list_tuple.append((list_a[i],list_b[i])) else: list_tuple.append((list_a[i],mask_str)) else: print "Bigger:Target Two." loop_count = len(list_b) print "循环上限:",loop_count for i in xrange(loop_count): if (i < len(list_a)): list_tuple.append((list_a[i],list_b[i])) else: list_tuple.append((mask_str,list_b[i])) print "生成后的列表:",list_tuple print "=== === ===" print "End." |
执行效果如下:
如果第一个列表较大:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
C:\Python27\python.exe D:/adamhuan_data/Python_Project/TwoList/TwoList.py ###################### Build List ###################### Input Your Mask String: * === === === List One Input Your Data(列表元素以‘,’分隔):adamhuan,1,2,3,5,allah ------------- The data is: ['adamhuan', '1', '2', '3', '5', 'allah'] ------------- === === === List Two Input Your Data(列表元素以‘,’分隔):Angela Baby,Amber,56,90,21 ------------- The data is: ['Angela Baby', 'Amber', '56', '90', '21'] ------------- === === === Size of Target One: 6 Size of Target Two: 5 差额不为:0,.. 需要用MASK补全。 Bigger:Target One. 循环上限: 6 生成后的列表: [('adamhuan', 'Angela Baby'), ('1', 'Amber'), ('2', '56'), ('3', '90'), ('5', '21'), ('allah', '*')] === === === End. Process finished with exit code 0 |
如果第二个列表较大:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
C:\Python27\python.exe D:/adamhuan_data/Python_Project/TwoList/TwoList.py ###################### Build List ###################### Input Your Mask String: * === === === List One Input Your Data(列表元素以‘,’分隔):adamhuan,31,90 ------------- The data is: ['adamhuan', '31', '90'] ------------- === === === List Two Input Your Data(列表元素以‘,’分隔):angela,baby,Hello world,4,6,9 ------------- The data is: ['angela', 'baby', 'Hello world', '4', '6', '9'] ------------- === === === Size of Target One: 3 Size of Target Two: 6 差额不为:0,.. 需要用MASK补全。 Bigger:Target Two. 循环上限: 6 生成后的列表: [('adamhuan', 'angela'), ('31', 'baby'), ('90', 'Hello world'), ('*', '4'), ('*', '6'), ('*', '9')] === === === End. Process finished with exit code 0 |
如果两个列表一样大:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
C:\Python27\python.exe D:/adamhuan_data/Python_Project/TwoList/TwoList.py ###################### Build List ###################### Input Your Mask String: * === === === List One Input Your Data(列表元素以‘,’分隔):adamhuan,1,2,3,4 ------------- The data is: ['adamhuan', '1', '2', '3', '4'] ------------- === === === List Two Input Your Data(列表元素以‘,’分隔):allah,5,6,7,8 ------------- The data is: ['allah', '5', '6', '7', '8'] ------------- === === === Size of Target One: 5 Size of Target Two: 5 差额为:0,.. 不需要补全。 循环上限: 5 生成后的列表: [('adamhuan', 'allah'), ('1', '5'), ('2', '6'), ('3', '7'), ('4', '8')] === === === End. Process finished with exit code 0 |
——————————————————————————
Done。