如何制作列表列表,你在哪里取每个偶数的平方和立方?
How to make list of lists, where you take the square and cube of each even number?
我想在对每个偶数进行平方和立方后制作一个列表列表。
以下是我到目前为止的代码:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all = list(ls2square +ls3cube)
return ls4all
RUN: sq_cube([1,2,3,4,6])
OUTPUT:[4, 16, 36, 8, 64, 216]
I would love my OUTPUT to be: [[4, 8], [16, 64], [36, 216]]
ls1: 这里我将列表1,2,3,4,6排序为偶数。
ls2square:对 ls1 中的偶数求平方。
ls3cube:对 ls1 中的偶数进行立方。
正如您在我的输出中看到的那样,它给出了两个列表,但没有给出每个列表
编号其单独的列表,其中偶数被平方和立方。
问题来自ls4all
。您的代码是
ls4all = list(ls2square +ls3cube)
变量 ls4all
不包含所需的列表列表:[[4, 8], [16, 64], [36, 216]]
因为它是 ls2square
和 ls3cube
的串联。我们更希望有 ls2square
和 ls3cube
中的元素对列表。为此,您可以创建一个迭代器来输出 ls2square
和 ls3cube
的元素对,例如,使用 zip
命令。
zip
命令是这样工作的:
>>> list(zip([1,10], [2,20]))
[(1, 2), (10, 20)]
如您所见,它将 [1,10]
和 [2,20]
中的元素聚集在一起,形成对。
所以你可以这样使用zip
:
ls2square = [4, 16, 36]
ls3cube = [8, 64, 216]
ls4all = list([a,b] for a,b in zip(ls2square, ls3cube))
print(ls4all)
无论如何,一个简短的答案是:
>>> print([(k**2, k**3) for k in [1,2,3,4,6] if k % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
更新:
如果您的整数类型为 float
, k % 2
将引发以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float modulo
因此,如果您想将浮点数四舍五入为最接近的整数,只需使用 round
函数,如下所示:
ls2square = [round(x)**2 for x in ls1]
ls3cube = [round(x)**3 for x in ls1]
上面的简短回答现在是:
>>> print([(round(k)**2, round(k)**3) for k in [1.0, 2.0, 3.0, 4.0, 6.0] if round(k) % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
因为你希望相同数量的正方形和立方体在同一个列表中,你可以使用这个,因为两个列表(ls2square,ls3cube)具有相同的长度而不是仅仅将它们相加你可以添加单独列出它们具有的每个对应元素,ls2square 中的元素 0 与 ls3cube 中的元素 0 等:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all= [[ls2square[k],ls3cube[k]] for k in range(len(ls3cube))]
return ls4all
创建一个新列表
轮入名单
循环每个数字以检查是否偶数
然后平方和立方那个数字
def sq_cube(numbers):
new_string = []
numbers = [round(num) for num in numbers]
for W in numbers:
if W % 2 ==0:
new_string.append([W**2,W**3])
return new_string
我想在对每个偶数进行平方和立方后制作一个列表列表。
以下是我到目前为止的代码:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all = list(ls2square +ls3cube)
return ls4all
RUN: sq_cube([1,2,3,4,6])
OUTPUT:[4, 16, 36, 8, 64, 216]
I would love my OUTPUT to be: [[4, 8], [16, 64], [36, 216]]
ls1: 这里我将列表1,2,3,4,6排序为偶数。
ls2square:对 ls1 中的偶数求平方。
ls3cube:对 ls1 中的偶数进行立方。
正如您在我的输出中看到的那样,它给出了两个列表,但没有给出每个列表 编号其单独的列表,其中偶数被平方和立方。
问题来自ls4all
。您的代码是
ls4all = list(ls2square +ls3cube)
变量 ls4all
不包含所需的列表列表:[[4, 8], [16, 64], [36, 216]]
因为它是 ls2square
和 ls3cube
的串联。我们更希望有 ls2square
和 ls3cube
中的元素对列表。为此,您可以创建一个迭代器来输出 ls2square
和 ls3cube
的元素对,例如,使用 zip
命令。
zip
命令是这样工作的:
>>> list(zip([1,10], [2,20]))
[(1, 2), (10, 20)]
如您所见,它将 [1,10]
和 [2,20]
中的元素聚集在一起,形成对。
所以你可以这样使用zip
:
ls2square = [4, 16, 36]
ls3cube = [8, 64, 216]
ls4all = list([a,b] for a,b in zip(ls2square, ls3cube))
print(ls4all)
无论如何,一个简短的答案是:
>>> print([(k**2, k**3) for k in [1,2,3,4,6] if k % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
更新:
如果您的整数类型为 float
, k % 2
将引发以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float modulo
因此,如果您想将浮点数四舍五入为最接近的整数,只需使用 round
函数,如下所示:
ls2square = [round(x)**2 for x in ls1]
ls3cube = [round(x)**3 for x in ls1]
上面的简短回答现在是:
>>> print([(round(k)**2, round(k)**3) for k in [1.0, 2.0, 3.0, 4.0, 6.0] if round(k) % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
因为你希望相同数量的正方形和立方体在同一个列表中,你可以使用这个,因为两个列表(ls2square,ls3cube)具有相同的长度而不是仅仅将它们相加你可以添加单独列出它们具有的每个对应元素,ls2square 中的元素 0 与 ls3cube 中的元素 0 等:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all= [[ls2square[k],ls3cube[k]] for k in range(len(ls3cube))]
return ls4all
创建一个新列表 轮入名单 循环每个数字以检查是否偶数 然后平方和立方那个数字
def sq_cube(numbers):
new_string = []
numbers = [round(num) for num in numbers]
for W in numbers:
if W % 2 ==0:
new_string.append([W**2,W**3])
return new_string