1:
请问,这个题目怎么做?
a=[[1,2], [3,4], [5,6], [7,8], [9,0]]
b=[8,7,9,7,9]
其中,
b[0]=8 对应着 a[0]=[1,2]
b[1]=7 对应着 a[1]=[3,4]
b[2]=9 对应着 a[2]=[5,6]
b[3]=7 对应着 a[3]=[7,8]
b[4]=9 对应着 a4]=[9,0]
把 b 从小到大排序,变成b=[7,7,8,9,9],现在希望原来的b的成员与a的成员之间的对应关系能够保留,也就是同时把a变成
a=[[3,4], [7,8], [1,2], [5,6], [9,0]] ,或者
a=[[7,8], [3,4], [1,2], [5,6], [9,0]] ,或者
a=[[3,4], [7,8], [1,2], [9,0], [5,6]] ,或者
a=[[7,8], [3,4], [1,2], [9,0], [5,6]],
这四个解答里面任何一个都可以。
请问,用最简洁的python语句怎样编写这个小程序???
输出a ,b:结果:
[[3, 4], [7, 8], [1, 2], [5, 6], [9, 0]]
[7, 7, 8, 9, 9]
这个题目怎么做?
实现:
a = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] b = [8, 7, 9, 7, 9] def func(a, b): new_list = zip(a, b) new_list.sort(key=lambda x: x[1]) a_new = [] b_new = [] for value_tuble in new_list: a_new.append(value_tuble[0]) b_new.append(value_tuble[1]) return a_new, b_new a, b = func(a, b) print a print b
2: 测试你的装饰器使用的情况
将0 到100 的列表[0,1,2,3,4, .....99] , 输出为4个元素一组的。
[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, 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]
[84, 85, 86, 87]
[88, 89, 90, 91]
[92, 93, 94, 95]
[96, 97, 98, 99]
# coding:utf-8 def decorator(func):
pass
@decorator
def print_4_list():
for i in range(100):
yield i
if __name__ == '__main__':
for x in print_4_list():
print x
assert isinstance(x, list)
实现:
# coding:utf-8 def decorator(func): def wrapper(*args, **kwargs): pre_index = 0 for iter_p in func(*args, **kwargs): if (iter_p + 1) % 4 == 0: yield list(func(*args, **kwargs))[pre_index:iter_p+1] pre_index = iter_p + 1 return wrapper @decorator def print_4_list(): for i in range(100): yield i if __name__ == '__main__': for x in print_4_list(): print x assert isinstance(x, list)