python zip

When I saw a senior who used a C ++ pair to process an x-axis array and a y-axis array as a set, I thought it was cool, but python also had a function called zip. I don't know

zip


x = [1, 2, 3]
y = [4, 5, 6]
z = zip(x, y)

You should now have [(1, 4), (2, 5), (3, 6)] assigned to z!

z


<zip object at 0x104d232c8>

that? It seems that the return value of the zip was changed from list in python2 to iterator in python3 ([Porting code to Python 3 using 2to3](http://diveintopython3-ja.rdy. jp / porting-code-to-python-3-with-2to3.html)).

zip2list


list(z) # [(1, 4), (2, 5), (3, 6)]

did it!

~~ I tried to find the same thing in PHP, but I didn't like it because only the function to compress it to Zip came out. ~~ In the comments, I told you that you can use array_map (). Thank you, knoguchi!

So I wrote it (although it's the same code as the comment).

array_map


$x = [1,2,3];
$y = [4,5,6];
$set = array_map(NULL, $x, $y);
var_dump($set);
/*
array(3) {
  [0]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    int(4)
  }
  [1]=>
  array(2) {
    [0]=>
    int(2)
    [1]=>
    int(5)
  }
  [2]=>
  array(2) {
    [0]=>
    int(3)
    [1]=>
    int(6)
  }
}
*/

I haven't mastered PHP yet ...

Recommended Posts

python zip
Zip, unzip with python
Python
zip
Python built-in functions ~ Zip ~
kafka python
[Python] What is a zip function?
Python basics ⑤
python + lottery 6
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python function ①
Python basics
Python memo
Recursively unzip zip files with python
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
[Python] Variables
Python functions
Python sys.intern ()
Python decimals
python underscore
Python summary
Recursive zip
Start python
[Python] Sort
Note: Python
Python basics ③
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
Python memorandum
python learning
Hannari Python 2020
python memorandum
Download python
python memorandum
Python memo
started python
Python #JSON
python quiz
Python note