I have been trying to learn a scripting language for a long time, but I haven't spare time. Recently I just got a job and need to use a lot of scripts. So I recently started to learn Python, put Hadoop in the first place.
We can use python to process the data, such as the following group of data:
The Holy Grail,1975,Terry Jones & Terry Gilliam,91 mins Graham chapman Michael Palin,John cleese,Terry Gilliam,Eric Idle & Terry JonesThe Life of Brain,1979,Terry Jones,94 mins Graham chapman Michael Palin,John cleese,Terry Gilliam,Eric Idle & Terry Jones
This set of data may be a bit complicated. However, this set of data has a certain structure: the first line shows the basic information of a group of movies, and the next line is the main actors, in the third line, the film's supporting role is listed.
How can we store this information?
Create a simple Python list. Here we can create a simple Python list to store movie names. The Code is as follows:
movies = ["The Holy Grail","The Life of Brain"]
In fact, the list here is like an array in Java or other programming languages. The following table also starts from 0. If we want to call the first data, then you can use movies [0] for calling. Similarly, you can use the print function of Python to print the data.
>>> print(movies[0])The Holy Grail
With a simple list, we will first introduce some basic operations on the Python list: You can directly print the list name to view all the content in the list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> print(movies)['The Holy Grail', 'The Life of Brain']>>>
You can also print the length of the current list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> print(len(movies))2>>>
Add a data entry to the end of the list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> movies.append("Hello")>>> print(movies)['The Holy Grail', 'The Life of Brain', 'Hello']>>> Delete a data entry from the end of the list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> movies.pop()'The Life of Brain'>>> print(movies)['The Holy Grail']>>>
You can also add a list at the end of the list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> movies.extend(["Hello","World"])>>> print(movies)['The Holy Grail', 'The Life of Brain', 'Hello', 'World']>>>
Find and delete a data item in the list:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> movies.remove("The Life of Brain")>>> print(movies)['The Holy Grail']>>> Add a data item before a specific position:
>>> movies = ["The Holy Grail","The Life of Brain"]>>> movies.insert(0,"Hello")>>> print(movies)['Hello', 'The Holy Grail', 'The Life of Brain']>>>
Before adding more data to the list, we have created a simple list and saved the name of the movie we want to store to it, now we can continue to add other information about the movie to the list, and then store the year of the movie. We can directly store it like this:
movies = ["The Holy Grail",1975,"The Life of Brain",1979]
Well, for convenience, we will first introduce how to use the content in the output list. Like the Java language, we can output the content in the list through a for loop, but the syntax format may be different, as shown below:
>>> movies = ["The Holy Grail",1975,"The Life of Brain",1979]>>> for item in movies:print(item)The Holy Grail1975The Life of Brain1979>>>
Note the syntax format. There is a ":" at the end of the for statement, and we can find that there is no {} pair after the for Loop in Java, which is controlled by indentation, therefore, the next print statement must be indented. Of course, we can also use the while loop to output the content in the list:
>>> movies = ["The Holy Grail",1975,"The Life of Brain",1979]>>> count = 0>>> while count < len(movies):print(movies[count])count = count+1The Holy Grail1975The Life of Brain1979>>>
The variable count is used to identify the number of accesses currently.
Store the list in the list. Now let's store the rest of the movie information:
movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]
There is no suspense here, but it is just to nest other lists in a list. You need to carefully understand this list, and then we can output the list through for, as shown below:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> for item in movies:print(item)The Holy Grail1975Terry Jones & Terry Gilliam91['Graham chapman', ['Michael Palin', 'John cleese', 'Terry Gilliam', 'Eric Idle & Terry Jones']]The Life of Brain1979Terry Jones94['Graham chapman', ['Michael Palin', 'John cleese', 'Terry Gilliam', 'Eric Idle & Terry Jones']]>>>
From the results, we can see that no nested content can be output normally, but if it is a nested list, it is output directly according to the list, but not one by one according to the content, is there any way to output nested content one by one? In the list, we can use an isinstance () method to identify the type of data in the current content. For example, we need to determine whether movices is a list type, therefore, we can use the isinstance (movices, list) to determine, while using the if .... the else statement is as follows:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> if isinstance(movies,list):print("true")else:print("false")true>>> After processing the multi-layer nested list, we can nest the list and output it as follows:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> for item_1 in movies:if isinstance(item_1,list):for item_2 in item_1:print(item_2)else:print(item_1)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapman['Michael Palin', 'John cleese', 'Terry Gilliam', 'Eric Idle & Terry Jones']The Life of Brain1979Terry Jones94Graham chapman['Michael Palin', 'John cleese', 'Terry Gilliam', 'Eric Idle & Terry Jones']>>>
From the results, we can see that we have processed the second layer nesting, but we have not processed the third layer nesting, so we still need to nest one layer of output, as shown below:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> for item_1 in movies:if isinstance(item_1,list):for item_2 in item_1:if isinstance(item_2,list):for item_3 in item_2:print(item_3)else:print(item_2)else:print(item_1)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>>
In this way, we can output all the nested content, but this output is relatively troublesome. we can define a function to solve this problem:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> def print_lol(movies):for item_1 in movies:if isinstance(item_1,list):for item_2 in item_1:if isinstance(item_2,list):for item_3 in item_2:print(item_3)else:print(item_2)else:print(item_1)>>> print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>>
However, the function at this time cannot adapt to all lists. We can use recursion to improve this function:
>>> movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam",91, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]], "The Life of Brain",1979,"Terry Jones",94, ["Graham chapman",["Michael Palin","John cleese","Terry Gilliam","Eric Idle & Terry Jones"]]]>>> def print_lol(movies):for item_1 in movies:if isinstance(item_1,list):print_lol(item_1)else:print(item_1)>>> print_lol(movies)The Holy Grail1975Terry Jones & Terry Gilliam91Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry JonesThe Life of Brain1979Terry Jones94Graham chapmanMichael PalinJohn cleeseTerry GilliamEric Idle & Terry Jones>>>
In this way, all list printing in python can use this function.