Fortune Telling Collection - Zodiac Guide - Python creates constellation list _python outputs constellation date correspondence table
Python creates constellation list _python outputs constellation date correspondence table
There are many methods built into lists in Python. Let's use "L" to represent a list and "X" to represent the parameters of the method to illustrate the usage of the list.
1 append () method
The append () method of the list is used to add an item at the end of the list, and L.append(x) is equivalent to L[len(L):] = [x].
For example, use the append () method to add "cow" and "elephant" to the end of the animal list, respectively:
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animals.append('cow ')? #? Equivalent to animal [4:]=[' cow']?
& gt& gt& gt? Animals?
【' cat',? Dog',? Fish? Dog',? Cattle']?
& gt& gt& gt? Animals.append ('elephant')? #? Equivalent to animal [5:]=[' elephant']?
& gt& gt& gt? Animals?
【' cat',? Dog',? Fish? Dog',? Cattle',? Elephant']?
2 () method
The () method of list is used to insert an item at the previous position of the specified index. L.(0, x) means inserting x before the list, and l. (len (l)) and x) are equivalent to L.append(x).
For example, use the () method to insert "cow" and "elephant" into the animal list respectively:
& gt& gt& gt? Animals? = ['cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animal (0,? Cattle')?
& gt& gt& gt? Animals?
【' cow',? "Cat"? Dog',? Fish? Dog']?
& gt& gt& gt? animal (3,? Elephant')?
& gt& gt& gt? Animals?
【' cow',? "Cat"? Dog',? An elephant? Fish? Dog']?
3 extend () method
The extend () method of the list is used to append all items of the iterable object to the list. L.extend(iterable) is equivalent to l [len (l):] = iteration. The difference between the extend () and append () methods is that the extend () method "extends" the iterable object.
For example, use the append () method and the extend () method to append the list containing "cow" and "elephant" to the animal list, respectively:
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animals.append(['cow ',? Elephant'])? #? Here the append () parameter is a list?
& gt& gt& gt? Animals?
【' cat',? Dog',? Fish? Dog',? 【' cow',? Elephant']]?
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animals.extend(['cow ',? Elephant'])? #? The extend () parameter here is also a list?
& gt& gt& gt? Animals?
【' cat',? Dog',? Fish? Dog',? Cattle',? Elephant']?
4 remove () method
The remove () method of the list is used to remove an item with a specified value from the list. L.remove(x) deletes the first item with the value of x in the list. If there is no item with the value x, a ValueError exception will be thrown.
For example, use the remove () method to delete an item with the value "dog" from the animal list:
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animals.remove('dog ')?
& gt& gt& gt? Animals?
【' cat',? Fish? Dog']?
& gt& gt& gt? animals.remove('dog ')?
& gt& gt& gt? Animals?
【' cat',? Fish']?
& gt& gt& gt? animals.remove('dog ')?
Retrospective? (Most? The nearest? Call? Finally):?
Documents? "",? Line? 1,? exist
Wrong value:? list.remove(x):? x? Isn't it? Are you online? List?
5 pop () method
The pop () method of the list is used to remove the item at the specified position in the list and return it. If no location is specified, L.pop () deletes and returns the last item in the list.
For example, use the pop () method to delete an item at a specified location in the animal list:
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? animals.pop()?
A dog?
& gt& gt& gt? Animals?
【' cat',? Dog',? Fish']?
& gt& gt& gt? animals.pop(2)?
Fish?
& gt& gt& gt? Animals?
【' cat',? Dog']?
After calling the previous list method, no value is printed, while the pop () method prints the "pop-up" value. Methods including append (), () and pop () are all "in-situ operations". Local operation (also called local operation) only modifies the list itself and does not return the modified list.
Both int () function and str () function used in type conversion have return values:
& gt& gt& gt? Number? =? 123?
& gt& gt& gt? My string? =? Str (number)? #? Assign the return value to the variable mystring?
& gt& gt& gt? My string?
' 123'?
However, when using "in-place operation", most of them will not return a value, including the pop () method, and only the "pop-up" value will be returned, and the modified list will not be returned:
& gt& gt& gt? Animals? =? 【' cat',? Dog',? Fish? Dog']?
& gt& gt& gt? New _ animal? =? animals.append('cow ')?
& gt& gt& gt? Print (New _ Animal)?
No?
For the basic questions about deep learning, you can look at the video tutorials and web links on this page. I hope my answer can help you.
- Related articles
- Fire king. Liang Ling's Xin Guo materials.
- Constellation pairing: Who is the best match of the twelve constellations?
- Reasons for lack of love in 12 constellation
- What happened, Kamen Rider?
- What kind of wedding does 12 constellation belong to _ What is the wedding of the twelve constellations like?
- What constellation is August 17? What is the character of this constellation?
- What constellation is called get up _ what constellation is it?
- 1992 What is the constellation of the Lunar New Year?
- Aries is not a good sign _ Aries is not a good sign?
- What is the Chinese zodiac in 69 years of the lunar calendar? What constellation is the ninth day of June?