有没有一种方法可以按顺序单独操作列表中的每个元素?

huangapple go评论75阅读模式
英文:

Is there a way to operate each element from a list separately in order?

问题

我有一个像这样的数组列表:

[1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]

我想逐个取出每个元素,并与其他数字进行算术运算。
代数版本是:

x^3+12x^2+48x+64

其中"x"是数组中在[i]位置的数字。

输出应该是:

[125, 216, 512, 1728, ..., -216000]

我尝试使用一个for循环,计算数组中元素的数量(在这种情况下为14),并将其用作范围。但我仍然收到“列表索引超出范围”的错误。

for i in range(numElements):
    x = factors[i]

如果您需要更多信息,请告诉我。

英文:

I have an array list like this:

[1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]

and I want to take each element one by one and operate them arithmetically with other numbers.
The algebraic version is:

x^3+12x^2+48x+64

Being "x" the number of the array in the [i] position

The output should be like:

[125, 216, 512, 1728, ..., -216000]

I tried with a for cycle counting the number of elements in the array (14 in this case) and using it as the range. But I still get the error "list index out of range".

for i in range(numElements):
            x = factors[i]

Please if you need more information tell me.

答案1

得分: 2

你可以定义一个函数来执行这个操作然后循环遍历你的列表在每个元素上调用该函数如下所示

```python
def operate(num):
  return num**3+12*num**2+48*num+64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]
output = [operate(num) for num in nums]

<details>
<summary>英文:</summary>

You can define a function to perform this operation. Then loop through your list, calling that function on each element like so:

def operate(num):
return num3+12*num2+48*num+64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]
output = [operate(num) for num in nums]


</details>



# 答案2
**得分**: 0

k = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]
result = [x**3 + 12*x**2 + 48*x + 64 for x in k]

<details>
<summary>英文:</summary>

    k=[1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64]    
    result=[x**3+12*x**2+48*x+64 for x in k]

</details>



# 答案3
**得分**: 0

我们可以为了方便定义一个函数,名为 `operate`。

```python
def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

选项 0 - 使用 for 循环

解决这个问题的最简单方法是使用一个 for 循环,就像您打算的那样。

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # 您的输入列表

result = []
for num in nums:
    result.append(operate(num))

如果您尝试访问超出列表长度的索引 i,可能会出现 "list index out of range" 错误。尝试使用 len(nums) 而不是手动计数。您可以编写 for 循环如下:

for i in range(len(nums)): 
    x = nums[i]

但如果您想更 "pythonic" 的话,最好使用上面的方法。

选项 1 - 使用列表推导式

我们可以将这个函数应用到 nums 列表的每个元素上。列表推导式文档

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # 您的输入列表
result = [operate(x) for x in nums]

选项 2 - 使用 map

另一种方法是使用 map 函数,它接受要应用于可迭代对象的 函数 文档

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # 您的输入列表
result = list(map(operate, nums))

选项 3 - 使用 lambda

如果您寻找一行的解决方案,可以考虑使用 lambda 函数map

result = list(map(lambda x: x ** 3 + 12 * x ** 2 + 48 * x + 64, nums))
英文:

We can define a function, operate for convenience.

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

Option 0 - Using for loop

The simplest way to solve this is to use a for loop as you intended.

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # your input list

result = []
for num in nums:
    result.append(operate(num))

You might be getting the error, "list index out of range", if you are trying to access the index, i, which is out of the length of the list. Try using len(nums) instead of manually counting. You can write the for loop as,

for i in range(len(nums)): 
    x = nums[i]

But its preferred to use the above method if you want to be more "pythonic".

Option 1 - Using list comprehension

We can apply this function to each of the elements of the nums list. List comprehension documentation.

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # your input list
result = [operate(x) for x in nums]

Option 2 - Using map

Another method is to use the map function, which takes in the function you want to apply on the iterable documentation.

def operate(x):
    return x ** 3 + 12 * x ** 2 + 48 * x + 64

nums = [1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64] # your input list
result = list(map(operate, nums))

Operation 3 - Using lambda

If you are looking for a one-liner solution, you should consider lambda functions with map.

result = list(map(lambda x: x ** 3 + 12 * x ** 2 + 48 * x + 64, nums))

huangapple
  • 本文由 发表于 2023年6月29日 13:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定