当维度不匹配时,将第一个添加到 python 列表
Add number one to a python list when the dimensions doesn't match
用不用numpy
重要吗?但是我尝试使用它并没有成功。
这是代码:
c = []
a = list(range(10))
c.append(a)
b = list(range(5))
c.append(b)
如何将1
添加到c
的所有元素?
你可以做到这一点。
c = [[sum(x) for x in zip(y, [1]*len(y))] for y in c]
我们可以使用两个列表理解来做你想做的事。外层循环遍历 c
中的列表,内层循环遍历 c
:
中每个列表中的值
c = []
a = list(range(10))
c.append(a)
b = list(range(5))
c.append(b)
[[val+1 for val in list] for list in c]
用不用numpy
重要吗?但是我尝试使用它并没有成功。
这是代码:
c = []
a = list(range(10))
c.append(a)
b = list(range(5))
c.append(b)
如何将1
添加到c
的所有元素?
你可以做到这一点。
c = [[sum(x) for x in zip(y, [1]*len(y))] for y in c]
我们可以使用两个列表理解来做你想做的事。外层循环遍历 c
中的列表,内层循环遍历 c
:
c = []
a = list(range(10))
c.append(a)
b = list(range(5))
c.append(b)
[[val+1 for val in list] for list in c]