学习python的第三十二天-列表,元组,字典

这一节内容书上写的很好,总结起来也没有什么,为了以后看的时候更方便些,大部分都是来自书中的原话。

列表

list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。一旦你创建了一个列表,你可以添加、删除或是搜索列表中的项目。由于你可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。

列表是使用对象和类的一个例子。当你使用变量 i 并给它赋值的时候,比如赋整数5,你可以认为你创建了一个类(类型) int 的对象(实例) iPythonlist 类提供了 append 方法,这个方法让你在列表尾添加一个项目。例如 mylist.append('an item') 列表 mylist 中增加那个字符串。注意,使用点号来使用对象的方法。

# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
    print item,

print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist

print 'The first item I will buy is', shoplist[0]
olditem = shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist

运行结果:

I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot',
'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango',
'rice']

元组

元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。

zoo = ('wolf', 'elephant', 'penguin')

print 'Number of animals in the zoo is', len(zoo)
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number  of  animals  in  the  new  zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last  animal  brought  from  old  zoo  is',
new_zoo[2][2]

运行结果:

Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin',
('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant',
'penguin')
Last animal brought from old zoo is penguin

元组最通常的用法是用在打印语句中,下面是一个例子:

age = 22
name = 'Swaroop'

print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name

运行结果:

Swaroop is 22 years old
Why is Swaroop playing with that python?

字典

字典的样子大概是这个样子的,名称对应内容。标记方式: d = {key1 : value1, key2 : value2 }

# 'ab' is short for 'a'ddress'b'ook
ab  =  { 'Swaroop' : '[email protected]',
         'Larry' : '[email protected]',
         'Matsumoto' : '[email protected]',
         'Spammer' : '[email protected]'
        }
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = '[email protected]'

# Deleting a key/value pair
del ab['Spammer']

print '\nThere are %d contacts in the address-book\n' %
len(ab)
for name, address in ab.items():
    print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
    print "\nGuido's address is %s" % ab['Guido']

运行结果:

Swaroop's address is [email protected]
There are 4 contacts in the address-book
Contact Swaroop at [email protected]
Contact Matsumoto at [email protected]
Contact Larry at [email protected]
Contact Guido at [email protected]
Guido's address is [email protected]

序列

这部分内容觉得比较重要,序列的主要内容是对列表,元组和字符串的索引和切片。

shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]

# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]

# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]

运行结果:

Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

它如何工作:

首先,我们来学习如何使用索引来取得序列中的单个项目。这也被称作是下标操作。每当你用方括号中的一个数来指定一个序列的时候,Python会为你抓取序列中对应位置的项目。记住,Python从0开始计数。因此, shoplist[0] 抓取第一个项目, shoplist[3] 抓取 shoplist 序列中的第四个元素。

索引同样可以是负数,在那样的情况下,位置是从序列尾开始计算的。因此, shoplist[-1] 表示序列的最后一个元素而 shoplist[-2] 抓取序列的倒数第二个项目。

切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的。

切片操作符中的第一个数(冒号之前)表示切片开始的位置,第二个数(冒号之后)表示切片到哪里结束。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。注意,返回的序列从开始位置 开始 ,刚好在 结束 位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。

这样, shoplist[1:3] 返回从位置1开始,包括位置2,但是停止在位置3的一个序列切片,因此返回一个含有两个项目的切片。类似地, shoplist[:] 返回整个序列的拷贝。

你可以用负数做切片。负数用在从序列尾开始计算的位置。例如, shoplist[:-1] 会返回除了最后一个项目外包含所有项目的序列切片。

使用Python解释器交互地尝试不同切片指定组合,即在提示符下你能够马上看到结果。序列的神奇之处在于你可以用相同的方法访问元组、列表和字符串。

发布了72 篇原创文章 · 获赞 42 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/A_lPha/article/details/53572804