列表是一系列元素的集合,我们应该能够获得集合的任意子集。对列表来说,我们应该能够获取列表的前3个,后3个,中间的任意3个连续的列表子集。这种获取任意 x 个连续列表元素的操作称之为切割(slices)。
为了获取到我们想得到的列表子集,我们应当给出子集中第一个想要的元素的位置和第一个不想要的元素的位置。因此 list[0 : 3] 会包含列表中元素 0,1和2,但不包括 3(数字代表索引)。
获取前三个元素的示例如下:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Grab the first three users in the list.
first_batch = usernames[0:3]
for user in first_batch:
print(user.title())
如果你想获取指定位置之前的所有元素,可以置空冒号前的第一个索引,如下所示:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Grab the first three users in the list.
first_batch = usernames[:3]
for user in first_batch:
print(user.title())
从列表中切割后,原列表不受影响,如下所示:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Grab the first three users in the list.
first_batch = usernames[0:3]
# The original list is unaffected.
for user in usernames:
print(user.title())
我们可以获取列表中任意元素段,如下所示:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Grab a batch from the middle of the list.
middle_batch = usernames[1:4]
for user in middle_batch:
print(user.title())
如果你想获取指定位置之后的所有元素,可以置空冒号后的索引,如下所示:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Grab all users from the third to the end.
end_batch = usernames[2:]
for user in end_batch:
print(user.title())
复制一个列表
利用切割操作可以复制一个列表,只需要置空冒号前后的索引,如下所示:
usernames = ['bernice', 'cody', 'aaron', 'ever', 'dalia']
# Make a copy of the list.
copied_usernames = usernames[:]
print("The full copied list:\n\t", copied_usernames)
# Remove the first two users from the copied list.
del copied_usernames[0]
del copied_usernames[0]
print("\nTwo users removed from copied list:\n\t", copied_usernames)
# The original list is unaffected.
print("\nThe original list:\n\t", usernames)
动手试一试
Alphabet Slices
创建一个包含英文前10个字母的列表。
打印列表中的前三个字母。
从列表中打印任意三个连续的字母。
打印从列表中某个位置开始的所有字母。
Protected List
这个练习的目的是证明复制一个列表对原列表不产生影响。
创建一个包含三个名字的列表。
使用切割的方法复制一个列表。
添加两个名字到新的列表中。
打印原列表中的名字,并打印一条语句说明这是原列表。
打印新列表中的名字,并打印一条语句说明这是新列表。
from string import ascii_lowercase
print(ascii_lowercase)
tenletters=ascii_lowercase[0:10]
print(tenletters)
# Ex: Alphabet Slices
#Store the first ten letters of the alphabet in a list.
alphabet=tenletters[:]
#Use a slice to print out the first three letters of the alphabet.
print(alphabet[:3])
#Use a slice to print out any three letters from the middle of your list.
print(alphabet[6:9])
#Use a slice to print out the letters from any point in the middle of your list, to the end.
print(alphabet[6:])
# put your code here
# Ex: Protected List
#Your goal in this exercise is to prove that copying a list protects the original list.
#Make a list with three people's names in it.
names=['alice','anna','ada']
#Use a slice to make a copy of the entire list.
copied_names=names[:]
#Add at least two new names to the new copy of the list.
copied_names.append('agata')
copied_names.append('aurora')
#Make a loop that prints out all of the names in the original list, along with a message that this is the original list.
print('This is the original list:')
for name in names:
print(name.title())
#Make a loop that prints out all of the names in the copied list, along with a message that this is the copied list.
print('This is the copy: ')
for cname in copied_names:
print(cname.title())
print(copied_names)
#title the names in the original list
print (names)
copied_names = [i.title() for i in copied_names]
print(copied_names)
网友评论