array_combine()来自Python中的PHP等价物?

时间:2021-11-08 12:21:57

I'm trying to do something like:

我正在尝试做类似的事情:

keys = [2,3,10]  # or even with assosiative keys like "orange", "apple", ..
vals = [7,9,11]
dict = array_combine(keys,vals)

Any way to implent that example as using array_combine() bult-in funcion of PHP5 ?

使用PHP5的array_combine()bult-in函数实现该示例的任何方法?

http://us3.php.net/manual/en/function.array-combine.php

http://us3.php.net/manual/en/function.array-combine.php

1 个解决方案

#1


1  

dict(zip(keys, vals))

Seems to be what you want.

似乎是你想要的。

Demo:

演示:

>>> keys = [2,3,10]
>>> vals = [7,9,11]
>>> dict(zip(keys, vals))
{2: 7, 3: 9, 10: 11}

zip puts elements at corresponding indices within the two lists into tuples. We can then put those pairs together into a new structure with the dict constructor.

zip将两个列表中相应索引处的元素放入元组中。然后我们可以使用dict构造函数将这些对组合到一个新结构中。

#1


1  

dict(zip(keys, vals))

Seems to be what you want.

似乎是你想要的。

Demo:

演示:

>>> keys = [2,3,10]
>>> vals = [7,9,11]
>>> dict(zip(keys, vals))
{2: 7, 3: 9, 10: 11}

zip puts elements at corresponding indices within the two lists into tuples. We can then put those pairs together into a new structure with the dict constructor.

zip将两个列表中相应索引处的元素放入元组中。然后我们可以使用dict构造函数将这些对组合到一个新结构中。