Kaggle课程官网链接:Intro to Lists
本专栏旨在Kaggle官方课程的汉化,让大家更方便地看懂。
整理您的数据,以便您能够高效地使用它。
在进行数据科学研究时,您需要一种组织数据的方法,以便高效地使用它。Python有许多数据结构可用于保存数据,如列表、集合、字典和元组。在本教程中,您将学习如何使用Python列表。
在“花瓣到金属”比赛中,你的目标是仅根据花朵的图像对花朵的种类进行分类。(这是计算机视觉中的一项常见任务,称为图像分类。)为了实现这一目标,假设你在数据中组织了花卉物种的名称。
一种方法是将名称组织在Python字符串中。
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)
pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,english marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle
更好的方法是在Python列表中表示相同的数据。要创建列表,您需要使用方括号([,])并用逗号分隔每个项目。列表中的每个项目都是Python字符串,因此每个项目都用引号括起来。
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)
['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle']
乍一看,无论您是用Python字符串还是列表表示信息,它看起来都没有太大不同。但正如你将看到的,有很多任务你可以用列表更容易地完成。例如,列表将使以下操作更容易:
在指定位置(第一、第二、第三等)获取项目,
检查项目数量,以及添加和删除项目。
我们可以使用len()来计算任何列表中的条目数量,len()是“length”的缩写。您只需在括号中提供列表的名称。
# The list has ten entries
print(len(flowers_list))
10
我们可以根据列表中的位置(第一、第二、第三等)引用列表中的任何项目。这被称为索引。
请注意,Python使用从零开始的索引,这意味着:
要提取列表中的第一个条目,您可以使用0,
要提取列表中的第二个条目,请使用1,然后
要提取列表中的最后一个条目,请使用比列表长度短的一个条目。
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])
First entry: pink primrose Second entry: hard-leaved pocket orchid Last entry: globe thistle
旁注:您可能已经注意到,在上面的代码单元格中,我们使用单个print()打印多个项目(包括Python字符串(如“First entry:”)和列表中的值(如flowers_list[0])。要用一个命令在Python中打印多个内容,我们只需要用逗号分隔它们。
您还可以提取列表的一部分(例如,前三个条目或后两个条目)。这被称为切片。例如:
要提取前x个条目,您可以使用[:x],以及
要提取最后的y个条目,可以使用[-y:]。
print("First three entries:", flowers_list[:3])
print("Final two entries:", flowers_list[-2:])
First three entries: ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells'] Final two entries: ['monkshood', 'globe thistle']
正如您在上面看到的,当我们切片一个列表时,它会返回一个新的、缩短的列表。
使用.Remove()从列表中删除一个项目,并将要删除的项目放在括号中。
flowers_list.remove("globe thistle")
print(flowers_list)
['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood']
使用.append()将项目添加到列表中,并将要添加的项目放在括号中。
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']
到目前为止,我们只使用了列表中每个项目都是字符串的列表。但是列表可以包含任何数据类型的项目,包括布尔值、整数和浮点数。
以2000年4月第一周某零售店的精装书销售为例。
hardcover_sales = [139, 128, 172, 139, 191, 168, 170]
在这里,hardver_sales是一个整数列表。与处理字符串时类似,您仍然可以执行诸如获取长度、拉取单个条目和扩展列表等操作。
print("Length of the list:", len(hardcover_sales))
print("Entry at index 2:", hardcover_sales[2])
Length of the list: 7 Entry at index 2: 172
你也可以用min()得到最小值,用max()得到最大值。
print("Minimum:", min(hardcover_sales))
print("Maximum:", max(hardcover_sales))
Minimum: 128 Maximum: 191
要添加列表中的每个项目,请使用sum()。
print("Total books sold in one week:", sum(hardcover_sales))
Total books sold in one week: 1107
我们也可以对列表的切片进行类似的计算。在下一个代码单元格中,我们取前五天的总和(sum(hardver_sales[:5])),然后除以五,得到前五天售出的平均书籍数量。
print("Average books sold in first five days:", sum(hardcover_sales[:5])/5)
Average books sold in first five days: 153.8
现在轮到你练习创建和修改列表了。