Python 相当于 php 的 foreach($array as $key => &$value)

发布于 2024-11-05 02:58:49 字数 253 浏览 0 评论 0原文

有没有与此 PHP 表示法等效的东西,它会更改原始数组(注意引用运算符)?

// increase value of all items by 1
foreach ($array as $k => &$v) {
    $v++;
}

我只知道这种方式,不太优雅:

for i in range(len(array)):
    array[i] += 1 

is there any equivalent to this PHP notation, which changes the original array (be aware of reference operator)?

// increase value of all items by 1
foreach ($array as $k => &$v) {
    $v++;
}

I know only this way, which is not so elegant:

for i in range(len(array)):
    array[i] += 1 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烙印 2024-11-12 02:58:49

当调用内置的 enumerate() 函数时在列表上,它返回一个可以迭代的对象,返回一个计数和从列表返回的值。

for i, val in enumerate(array):
    array[i] += 1

When the built in enumerate() function is called on a list, it returns an object that can be iterated over, returning a count and the value returned from the list.

for i, val in enumerate(array):
    array[i] += 1
夜吻♂芭芘 2024-11-12 02:58:49

您可以使用列表理解

newArray = [i + 1 for i in array]

You could use list comprehension:

newArray = [i + 1 for i in array]
自我难过 2024-11-12 02:58:49

关于参考文献。

在Python中,每个值都是对象,每个“变量”都是对该对象的引用。赋值从不复制值,它总是分配引用。

因此,默认情况下,v in for k,v in enumerate([1,2,3]) 也是引用。然而,大多数基本“类型”的对象都是不可变的,因此当您执行 immutable_object_reference += 1 时,您将创建 int 的新实例并将 immutable_object_reference 更改为指向新实例。

当我们的值是可变类型时,引用的工作方式与 PHP 中相同:

>>> class mutable_pseudoint(object):
...     def __init__(self, d):
...         self.d = d
...     def __iadd__(self, v):
...         self.d += v
...     def __repr__(self):
...         return self.d.__repr__()
...     def __str__(self):
...         return self.d.__str__()
... 
>>> l = [mutable_pseudoint(1), mutable_pseudoint(2), mutable_pseudoint(3), mutable_pseudoint(4)]
>>> l
[1, 2, 3, 4]
>>> for k,v in enumerate(l):
...     v += 1
... 
>>> l
[2, 3, 4, 5]

Regarding references.

In python, every value is object, and every 'variable' is reference to the object. Assignment never copies value, it always assigns reference.

So v in for k,v in enumerate([1,2,3]) is reference too, by default. However most objects of basic 'types' are immutable, therefore when you do immutable_object_reference += 1 you create new instance of int and change immutable_object_reference to point to new instance.

When our values are of mutable types, references work same as in PHP:

>>> class mutable_pseudoint(object):
...     def __init__(self, d):
...         self.d = d
...     def __iadd__(self, v):
...         self.d += v
...     def __repr__(self):
...         return self.d.__repr__()
...     def __str__(self):
...         return self.d.__str__()
... 
>>> l = [mutable_pseudoint(1), mutable_pseudoint(2), mutable_pseudoint(3), mutable_pseudoint(4)]
>>> l
[1, 2, 3, 4]
>>> for k,v in enumerate(l):
...     v += 1
... 
>>> l
[2, 3, 4, 5]
人生百味 2024-11-12 02:58:49

我不知道能够获取指向列表项的指针,但是 http://effbot.org/zone/python-list.htm

for index, object in enumerate(L):
    L[index] = object+1

I'm unaware of being able to get a pointer to a list item, but a cleaner way to access by index is demonstrated by http://effbot.org/zone/python-list.htm:

for index, object in enumerate(L):
    L[index] = object+1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文