Kaggle Learn(1):Basic Python
Intro to Lists:
statuation:
In the Petal to the Metal competition, your goal is to classify the species of a flower based only on its image. (This is a common task in computer vision, and it is called image classification.) Towards this goal, say you organize the names of the flower species in the data.
flowers = "pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,english marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle"
print(type(flowers))
print(flowers)
Python List(列表):
with [ , ]
Every item in the list is a Python string, so each is enclosed in quotation marks.
flowers_list = ["pink primrose", "hard-leaved pocket orchid", "canterbury bells", "sweet pea", "english marigold", "tiger lily", "moon orchid", "bird of paradise", "monkshood", "globe thistle"]
print(type(flowers_list))
print(flowers_list)
List function:
length:
len( list_name )
Indexing:
zero-based indexing.
print("First entry:", flowers_list[0])
print("Second entry:", flowers_list[1])
# The list has length ten, so we refer to final entry with 9
print("Last entry:", flowers_list[9])
To print multiple things in Python with a single command, we need only separate them with a comma.
Slicing:
To pull a segment of a list.
- to pull the first
<font style="color:rgb(60, 64, 67);">x</font>entries, you use<font style="color:rgb(60, 64, 67);">[:x]</font>, and
(要获取前<font style="color:rgb(60, 64, 67);">x</font>条记录,你使用<font style="color:rgb(60, 64, 67);">[:x]</font>) - to pull the last
<font style="color:rgb(60, 64, 67);">y</font>entries, you use<font style="color:rgb(60, 64, 67);">[-y:]</font>.
(要获取后<font style="color:rgb(60, 64, 67);">y</font>条记录,你使用<font style="color:rgb(60, 64, 67);">[-y:]</font>)
print("First three entries:", flowers_list[:3])
print("Final two entries:", flowers_list[-2:])
#output:
#First three entries: ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells']
#Final two entries: ['monkshood', 'globe thistle']
returns a new , shortened list .
Removing items:
.remove( _item_name_** )**
flowers_list.remove("globe thistle")
print(flowers_list)
#output:
#['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood']
Adding items:
.append ( _item_ name _)
flowers_list.append("snapdragon")
print(flowers_list)
#['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'snapdragon']
Lists are not just for strings
lists can have items with any data type, including booleans, integers, and floats.
You can also get the minimum with <font style="color:rgb(60, 64, 67);">min()</font> and the maximum with <font style="color:rgb(60, 64, 67);">max()</font>.
To add every item in the list, use <font style="color:rgb(60, 64, 67);">sum()</font>.
We can also do similar calculations with slices of the list. In the next code cell, we take the sum from the first five days (<font style="color:rgb(60, 64, 67);">sum(hardcover_sales[:5])</font>), and then divide by five to get the average number of books sold in the first five days.
print("Average books sold in first five days:", sum(hardcover_sales[:5])/5)
#Average books sold in first five days: 153.8
You can actually use Python to quickly turn this string into a list with <font style="color:rgba(0, 0, 0, 0.87);background-color:rgb(238, 238, 238);">.split()</font>. In the parentheses, we need to provide the character should be used to mark the end of one list item and the beginning of another, and enclose it in quotation marks. In this case, that character is a comma.
flowers = "pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,english marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle"
print(flowers.split(","))
#['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle']
Attention:
- First, about the return items : flowers_list[:3] returns a list ; but flowers_list[3] return a list item. If we want to caculate the items , we should pay attention to the forms of data.
- Next sum( item_name ),caculate the item. And split make work to String !But NOT the list !!! Because of split() returns a list ,String.split( "a" ).split("b" ) is error.

浙公网安备 33010602011771号