Reference site: [Introduction to Python] Let's use foreach with Python
Foreach is a repetitive statement that is often used in scripting languages. It is very often used when you want to take the contents out of an array and process them repeatedly. When using a script language, it is often desirable to have a uniform process, so if you can master repetitive statements, workability will definitely improve.
First of all, let's write a little foreach statement in perl, which is a typical scripting language.
my @sports = ("baseball", "tennis", "soccer", "volleyball");
foreach my $name (@sports){
print "$name\n"
}
$ perl foreach.pl
baseball
tennis
soccer
volleyball
In the foreach statement, we access from the first of the array called sports and continue to display the elements until we reach the last element.
table of contents 1 [Let's try foreach with python](Let's try foreach with python) 2 [Tips of For statement: Master more convenient usage with the feeling of foreach](Tips of For statement: Master more convenient usage with the feeling of foreach) 2.1 [I want to display the contents of the array in reverse order](I want to display the contents of the array in reverse order) 2.2 [I want to display the contents of the array in ascending order](I want to display the contents of the array in ascending order) 2.3 [I want to display the contents of the array in descending order](I want to display the contents of the array in descending order) 2.4 [I want to know the element number](I want to know the element number)]
Now let's try foreach with python! That said, python doesn't actually have a function named foreach. So what to do is that the for statement takes over.
Let's write it concretely.
>>> sports = ["baseball", "tennis", "soccer", "volleyball"]
>>> for name in sports:
... print name
...
baseball
tennis
soccer
volleyball
In this way, you can execute repeated statements in python with almost the same description as foreach.
If you try to display the contents of the array in reverse order with the script language such as perl so far, -Repack the array in reverse order and display it. -Obtain the array number and access it from the back number, There are various methods such as, but it is troublesome. Let me give you an example that you can easily do without doing this.
>>> sports = ["baseball", "tennis", "soccer", "volleyball"]
>>> for name in reversed(sports):
... print name
...
volleyball
soccer
tennis
baseball
You can easily reverse the order just by adding reversed () to the array to be read in this way.
You may want to display an array of disjointed numbers in ascending order. In that case, try the following.
>>> numbers = [8, 2, 7, 4, 3]
>>> for num in sorted(numbers):
... print num
...
2
3
4
7
8
You can easily sort by using the sorted function like this.
In the previous example, it was displayed in ascending order. However, in some cases, you may want to sort in descending order. In that case, you can write as follows.
>>> numbers = [8, 2, 7, 4, 3]
>>> for num in sorted(numbers, reverse=True):
... print num
...
8
7
4
3
2
>>>
You can sort in descending order by setting the keyword reverse of the sorted function to True.
You may want to know the number of the array element you are currently accessing. In such a case, there are two methods.
・ Method using range function
>>> sports = ["baseball", "tennis", "soccer", "volleyball"]
>>> for i in range(0, len(sports)):
... print i, sports[i]
...
0 baseball
1 tennis
2 soccer
3 volleyball
This is a C language tic feeling. This is how to use the range function to determine the element number of the array to be accessed. It's the right way to do it, but it's a little annoying. If you want to proceed easily with foreach ticks as you learned this time, why not try the following method.
・ Method using the enumerate function
>>> sports = ["baseball", "tennis", "soccer", "volleyball"]
>>> for i, name in enumerate(sports):
... print i, name
...
0 baseball
1 tennis
2 soccer
3 volleyball
Unlike using the range function earlier, it would be nice to be able to write in a refreshing description without having to describe where to display the array.
I want to display two arrays together If you have two arrays and want to process them together, it is convenient to use the zip function. An example looks like this:
>>> sports = ["baseball", "tennis", "soccer", "volleyball"]
>>> people = ["Ichiro", "Nishikori", "Nakata"]
>>> for name, person in zip(sports, people):
... print name, person
...
baseball Ichiro
tennis Nishikori
soccer Nakata
The miso here is that even if the number of elements in the array is different, it should be processed automatically according to the shorter one.
Recommended Posts