Python对应于php的foreach($array作为$key => &$value)

时间:2021-08-26 22:08:54

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

是否有与这个PHP表示法等价的,它改变了原始数组(注意引用操作符)?

// 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 

4 个解决方案

#1


5  

You could use list comprehension:

你可以使用列表理解:

newArray = [i + 1 for i in array]

#2


47  

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

#3


2  

Regarding references.

关于引用。

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

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

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.

所以v在k中,v在枚举中([1,2,3])也是参考,默认情况下。但是,基本“类型”的大多数对象都是不可变的,因此当您执行immutable_object_reference += 1时,您将创建int的新实例,并更改immutable_object_reference以指向新的实例。

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

当我们的值是可变类型时,引用的工作方式与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]

#4


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:

我不知道是否能够获得一个指向列表项的指针,但是通过http://effbot.org/zone/python/pythonlist.htm演示了一种更干净的访问索引的方法。

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

#1


5  

You could use list comprehension:

你可以使用列表理解:

newArray = [i + 1 for i in array]

#2


47  

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

#3


2  

Regarding references.

关于引用。

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

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

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.

所以v在k中,v在枚举中([1,2,3])也是参考,默认情况下。但是,基本“类型”的大多数对象都是不可变的,因此当您执行immutable_object_reference += 1时,您将创建int的新实例,并更改immutable_object_reference以指向新的实例。

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

当我们的值是可变类型时,引用的工作方式与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]

#4


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:

我不知道是否能够获得一个指向列表项的指针,但是通过http://effbot.org/zone/python/pythonlist.htm演示了一种更干净的访问索引的方法。

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