This is Qiita's first post. I would appreciate it if you would kindly watch over me. I decided to work with python in my university research, and I decided to output it in case I forgot what I had stumbled upon or learned because I had never touched programming. I hope you can help someone.
After learning python, the zip function came out, but when I first saw it, it was "What is this". When I look it up
"The zip function is a function to use when you want to get multiple lists at the same time." Https://www.sejuku.net/blog/66268
Hmmmm ... it's difficult for me. For the time being, can I get multiple lists at the same time ... but I don't know when to actually use it and what to do! That's why I've been thinking about the good points of the zip function. I think that the zip function can simplify the code, so I would like to write about the case where the zip function is used and the case where it is not used for the problem that I actually want to execute.
I want to know each person's name and gender and where they live at once.
If you try to write code without using the zip function, it will look like this.
no_zip.py
name = ['Noah','Emma','James']
gender = ['men','woman','men']
address = ['tokyo','chiba','nagoya']
for i in range(len(name)):
print(name[i],gender[i],address[i])
The output is
Noah men tokyo
Emma woman chiba
James men nagoya
It will be. First, create an array and rotate (0 to 2) as many times as the number of variables name in the for statement. Therefore, other variables gender and address also output the corresponding elements.
Next is the case when dealing with the zip function.
yes_zip.py
name = ['Noah','Emma','James']
gender = ['men','woman','men']
address = ['tokyo','chiba','nagoya']
for n,g,a in zip(name,gender,address):
print(n,g,a)
The output is
Noah men tokyo
Emma woman chiba
James men nagoya
It will be. I think the strange part is the for statement. In the name of the zip function, the first'Noah' in the array is picked up and put in n, and in the gebder, the first'men' is picked up and put in g. That is, the zip function can "get the index of more than one list".
This is Qiita's first post, and it may be difficult to understand because I have never sent information such as blogs. From now on, I would like to disseminate information and improve my writing skills, and my dream is to become a person who can play with technology, so please watch with warm eyes. Thank you in advance.
Recommended Posts