【发布时间】:2017-07-05 09:13:01
【问题描述】:
考虑下面的代码:
list1 = ['1x', '2x']
list2 = ['x18', 'x74']
list3 = [('100p1', '100p2'), ('300p1', '300p2')]
gen_list = [[a,b] for a in list1 for b in list2]
for new_list in gen_list:
for c in list3:
print(new_list.extend(c))
我的目标结果是这样的:
[['1x','x18, '100p1', '100p2'],
['1x','x74, '100p1', '100p2'],
['1x','x18, '300p1', '300p2'],
['1x','x74, '300p1', '300p2'],
['2x','x18, '100p1', '100p2'],
['2x','x74, '100p1', '100p2'],
['2x','x18, '300p1', '300p2'],
['2x','x74, '300p1', '300p2']]
但是上面代码的结果是这样的:
None
None
None
None
None
None
None
None
我需要对我的代码进行哪些必要的更正?提前致谢。
【问题讨论】:
-
print(new_list.extend(c))打印出None,因为extends返回None。只需在循环之后打印new_list。
标签: python list python-3.x tuples