1. 程式人生 > >Python 筆記 #02#

Python 筆記 #02#

sta val slice exe work some .com area color

源:DataCamp

datacamp 的 DAILY PRACTICE + 日常收集。

List of lists

Subset and conquer

Slicing and dicing

List Manipulation

List of lists

As a data scientist, you‘ll often be dealing with a lot of data, and it will make sense to group some of this data.

Instead of creating a flat list containing strings and floats, representing the names and areas of the rooms in your house, you can create a list of lists. The script on the right can already give you an idea.

Don‘t get confused here: "hallway" is a string, while hall is a variable that represents the float 11.25 you specified earlier.

# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50

# house information as list of lists
house = [["hallway", hall],
         [
"kitchen", kit], ["living room", liv], ["bedroom", bed], ["bathroom", bath]] # Print out house print(house) # Print out the type of house print(type(house))

Subset and conquer

一個元素毫無疑問也是一個子集,因此不論是取一段或者隨機訪問一個元素都叫做 subsetting

# Create the areas list
areas = ["
hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50] # Print out second element from areas print(areas[1]) # Print out last element from areas print(areas[-1]) # Print out the area of the living room print(areas[5])

#Subset and calculate

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Sum of kitchen and bedroom area: eat_sleep_area
eat_sleep_area = areas[3] + areas[-3]

# Print the variable eat_sleep_area
print(eat_sleep_area)

Slicing and dicing

技術分享圖片

Selecting single values from a list is just one part of the story. It‘s also possible to slice your list, which means selecting multiple elements from your list. Use the following syntax:

my_list[start:end]

The start index will be included, while the end index is not.

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Use slicing to create downstairs
downstairs = areas[0:6]

# Use slicing to create upstairs
upstairs = areas[6:10]

# Print out downstairs and upstairs
print(downstairs)
print(upstairs)

However, it‘s also possible not to specify these indexes. If you don‘t specify the begin index, Python figures out that you want to start your slice at the beginning of your list. If you don‘t specify the end index, the slice will go all the way to the last element of your list.

# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Alternative slicing to create downstairs
downstairs = areas[:6]

# Alternative slicing to create upstairs
upstairs = areas[6:]

List Manipulation

1、反向選就不用考慮 0 了,數到幾就是幾

2、如下操作也可以增加元素

In [3]: x
Out[3]: [a, b, s, t]
In [4]: x[2:] = ["s", "t", "hi"]
In [5]: x
Out[5]: [a, b, s, t, hi]

Extend a list

# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
         "bedroom", 10.75, "bathroom", 10.50]

# Add poolhouse data to areas, new list is areas_1
areas_1 = areas + ["poolhouse", 24.5]

# Add garage data to areas_1, new list is areas_2
areas_2 = areas_1 + ["garage", 15.45]

Delete list elements

del(areas[-4:-2])

Inner workings of lists

Change the second command, that creates the variable areas_copy, such that areas_copy is an explicit copy of areas

# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Create areas_copy
# areas_copy = areas 
areas_copy = list(areas)

# Change areas_copy
areas_copy[0] = 5.0

# Print areas
print(areas)

areas_copy = areas[:] 也是可以的!

Python 筆記 #02#