Traditionally, append () is used to add elements to the list one by one.
>>> marxes=["TTTTTT","aaaa","bbbb"]
>>> marxes.append("Zeppo")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo']
You can combine two ** lists ** with extend ().
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo']
>>> others=["Gummo","Karl"]
>>> marxes.extend(others)
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Zeppo', 'Gummo', 'Karl']
>>> marxes=["TTTTTT",'aaaa', 'CCCC']
>>> others=["Gummo","Karl"]
>>> marxes+=others
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', 'Karl']
At this time, if append () is used, it will be added as one list element instead of other elements. This also shows that the list can have different types.
>>> marxes=["TTTTTT",'aaaa', 'CCCC']
>>> others=["Gummo","Karl"]
>>> marxes.append(others)
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', ['Gummo', 'Karl']]
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', ['Gummo', 'Karl']]
The append () function could only add an element to the end of the list, but if you want to specify an offset for the list and add an element before it, use the insert () function.
>>> marxes.insert(3,"Gummo")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', ['Gummo', 'Karl']]
#If you specify an offset that exceeds the end, append()It is inserted at the end of the list in the same way as.
>>> marxes.insert(10,"XXXXXX")
>>> marxes
['TTTTTT', 'aaaa', 'CCCC', 'Gummo', ['Gummo', 'Karl'], 'XXXXXX']
>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> del marxes[-1]
>>> marxes
['Groucho', 'Choico', 'Haapo', 'Gummo']
>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> marxes[2]
'Haapo'
>>> del marxes[2]
>>> marxes
['Groucho', 'Choico', 'Gummo', 'Zepppo']
Since del is a Python statement, not a list method, we don't write marxes [-2] .del (). del is the opposite of assignment, separating the name from the Python object and freeing the object's memory if the name is the last reference to the object.
If you don't care about the position of the element you want to remove, use remove () to specify a value and remove the element.
>>> marxes=["Groucho","Choico", 'Haapo',"Gummo","Zepppo"]
>>> marxes.remove("Gummo")
>>> marxes
['Groucho', 'Choico', 'Haapo', 'Zepppo']
You can use pop () to pop an element out of the list and remove it from the list at the same time.
Calling pop () with an offset returns the element for that offset. If no argument is specified, -1 is used as the offset. pop (0) returns the list head, pop () or pop (-1) returns the end.
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> marxes.pop()
'Zepppo'
>>> marxes
['Groucho', 'Choico', 'Haapo']
>>> marxes.pop(1)
'Choico'
>>> marxes
['Groucho', 'Haapo']
If you want to know the offset in the list of elements from the value of the element, use index ().
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> marxes.index("Choico")
1
Use ** in ** to test if the list has a value, like Python.
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo"]
>>> "Groucho" in marxes
True
>>> "Boss" in marxes
False
Use count () to count how many specific values are in the list.
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo","Zepppo"]
>>> "Zepppo" in marxes
True
>>> marxes=["Groucho","Choico", 'Haapo',"Zepppo","Zepppo"]
>>> marxes.count("Groucho")
1
>>> marxes.count("Boss")
0
>>> marxes.count("Zepppo")
2
Just remember that "join () is the opposite of split ()"
>>> marxes=["Groucho","Choico", 'Haapo']
>>> ",".join(marxes)
'Groucho,Choico,Haapo'
>>> friends=["Harry", "Hermione", "Ron"]
>>> separator=" * "
>>> joined = separator.join(friends)
>>> joined
'Harry * Hermione * Ron'
>>> separated = joined.split(separator)
>>> separated
['Harry', 'Hermione', 'Ron']
>>> separated == joined
True
--The list function sort () sorts the list itself ** on the fly **. --The general function sorted () returns a copy of the sorted list.
If the elements of the list are numbers, they are sorted in ascending order by default. If the element is a string, it will be sorted alphabetically.
>>> marxes=["Groucho","Choico", 'Haapo']
#sorted_The marxes are copies, and making them does not change the original list.
>>> sorted_marxes = sorted(marxes)
>>> sorted_marxes
['Choico', 'Groucho', 'Haapo']
>>> marxes
['Groucho', 'Choico', 'Haapo']
#list function sort from marxes list()Calls to change the marxes list itself.
>>> marxes.sort()
>>> marxes
['Choico', 'Groucho', 'Haapo']
>>> numbers = [2,4.5,1,3]
>>> numbers.sort()
>>> numbers
[1, 2, 3, 4.5]
>>> numbers = [2,4.5,1,3]
#The default is ascending, but reverse=If you add the True argument, it will be in descending order.
>>> numbers.sort(reverse=True)
>>> numbers
[4.5, 3, 2, 1]
>>> numbers = [2,4.5,1,3]
len () returns the number of elements in the list.
>>> marxes=["Groucho","Choico", 'Haapo']
>>> len(marxes)
3
If you assign one list to multiple variables, when you rewrite the list with one of them, the other list will be rewritten.
>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> b = a
>>> b
[1, 2, 3]
>>> a[0]="surprise"
>>> b
['surprise', 2, 3]
>>> a
['surprise', 2, 3]
>>> b[0]="I hate surprises"
>>> b
['I hate surprises', 2, 3]
>>> a
['I hate surprises', 2, 3]
b refers to the same list object as a. Regardless of which name is used for a or b to rewrite the contents of the list, the operation is reflected in both.
You can copy the list to a new independent list using one of the following methods: --List copy () function --list () conversion function --List slice
>>> a=[1,2,3]
#b,c,d is a copy of a
>>> b=a.copy()
>>> c=list(a)
>>> d=a[:]
>>> a[0] = "integer lists are boring"
>>> a
['integer lists are boring', 2, 3]
>>> b
[1, 2, 3]
>>> c
[1, 2, 3]
>>> d
[1, 2, 3]
Withdrawal for one day is difficult. I wonder if I'll go out tomorrow even at noon.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts