Python 筆記 #02#

來源:互聯網
上載者:User

標籤:就是   col   comm   square   ever   inner   pst   slice   make   

源: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.25kit = 18.0liv = 20.0bed = 10.75bath = 9.50# house information as list of listshouse = [["hallway", hall],         ["kitchen", kit],         ["living room", liv],         ["bedroom", bed],         ["bathroom", bath]]# Print out houseprint(house)# Print out the type of houseprint(type(house))

 

Subset and conquer

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

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

#Subset and calculate

# Create the areas listareas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Sum of kitchen and bedroom area: eat_sleep_areaeat_sleep_area = areas[3] + areas[-3]# Print the variable eat_sleep_areaprint(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 listareas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Use slicing to create downstairsdownstairs = areas[0:6]# Use slicing to create upstairsupstairs = areas[6:10]# Print out downstairs and upstairsprint(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 listareas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]# Alternative slicing to create downstairsdownstairs = areas[:6]# Alternative slicing to create upstairsupstairs = areas[6:]

 

List Manipulation

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

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

In [3]: xOut[3]: [‘a‘, ‘b‘, ‘s‘, ‘t‘]In [4]: x[2:] = ["s", "t", "hi"]In [5]: xOut[5]: [‘a‘, ‘b‘, ‘s‘, ‘t‘, ‘hi‘]
Extend a list
# Create the areas list and make some changesareas = ["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_1areas_1 = areas + ["poolhouse", 24.5]# Add garage data to areas_1, new list is areas_2areas_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 areasareas = [11.25, 18.0, 20.0, 10.75, 9.50]# Create areas_copy# areas_copy = areas areas_copy = list(areas)# Change areas_copyareas_copy[0] = 5.0# Print areasprint(areas)

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

Python 筆記 #02#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.