Source: Datacamp
Datacamp DAILY Practice + daily collection.
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'll make sense to group some of the 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 a idea.
Don ' t get confused here: "hallway" was a string, while was hall a variable that represents the float you 11.25 specified Earlie R.
#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", Liv], ["Bedroom", bed], ["bathroom", Bath]]#Print out HousePrint(House)#Print out the type of housePrint(Type (house))
Subset and Conquer
An element is undoubtedly a subset, so either taking a section or randomly accessing an element is called subsetting
#Create the areas listareas = ["Hallway", 11.25,"Kitchen", 18.0,"Living", 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 livingPrint(Areas[5])
#Subset and calculate
#Create the areas listareas = ["Hallway", 11.25,"Kitchen", 18.0,"Living", 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 are 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]
startthe index would be included and while the end index was not.
#Create the areas listareas = ["Hallway", 11.25,"Kitchen", 18.0,"Living", 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 begin the index, the Python figures-out is want to start your slice at the beginning of the your list. If you don ' t specify end the index, the slice would go all the same as the last element of the your list.
#Create the areas listareas = ["Hallway", 11.25,"Kitchen", 18.0,"Living", 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, the reverse election will not have to consider 0, the number to a few is a few
2, the following operation can also add elements
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, which creates the variable areas_copy , such that's an areas_copy explicit copy ofareas
# Create List areasareas = [11.25, 18.0, 20.0, 10.75, 9.50]# Create areas_copy #areas_copy = list (areas)# Change areas_copyareas_copy[0] = 5.0 # print areasprint(areas)
Areas_copy = areas[:] is also possible!
Python Notes #02 #