python_数据结构

1.列表

#!/usr/bin/python
#
Filename: using_list.py

##################################
def showShopList(shoplist):
for item in shoplist:
print item,
#################################

shoplist
= ['apple', 'mango', 'carrot', 'banana']

print 'I have', len(shoplist), 'items to purchase.'
print 'These items are:', #Notice the comma at the end of the line
showShopList(shoplist)

print '\nI also have to buy rice.'
shoplist.append(
'rice')
print 'My shopping list is now:',showShopList(shoplist)

print 'I will sort my shoplist'
shoplist.sort()
print 'Sorted shopping list is:',showShopList(shoplist)
print 'The first item I will buy is', shoplist[0]
olditem
= shoplist[0]
del shoplist[0]
print 'I bought the', olditem

print 'Now I have', len(shoplist), 'items to purchase'
print 'My shopping list is now:', showShopList(shoplist)
print 'The second item I will buy is', shoplist[0]
root@sgb-desktop:~/program# python using_list.py
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 None
I will sort my shoplist
Sorted shopping list
is apple banana carrot mango rice None
The first item I will buy
is apple
I bought the apple
Now I have
4 items to purchase
My shopping list
is now banana carrot mango rice None
The second item I will buy
is banana

2.元组

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

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

#!/usr/bin/python
#
coding: utf-8 #为了在此文件中包含中文
#
Filename: print_tuple.py

age
= 22
name
= 'Swaroop'

print '%s is %d years old' % (name, age) #括号以及其内的数据合称之为元>组
print 'Why is %s playing with that python?' % name #单个字符同样可以使用此格式
这个有点类似于C的输出,不过python中好像给括号多了个定义,叫元组

3.字典

字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把(名字)和(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。

注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键
#!/usr/bin/python
#
Filename: using_dict.py

# 'ab' is short for 'a'ddress'b'ook

ab
= {
'Swaroop' : 'Swaroopch@byteofpython.info',
'Larry': 'larry@wall.org',
'Matsumoto' : 'matz@ruby-lang.org',
'Spammer' : 'spammer@hotmail.com'
}

print 'Swaroop\'s address is %s ' % ab['Swaroop']

#Adding a key/value pair
ab['Guido'] = 'guido@python.org'

#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 :
#
print 'Contact Guido at%s' % ab['Guido']

if ab.has_key('Guido'):
print 'Contact Guido at %s' % ab['Guido']
root@sgb-desktop:~/program# python using_dict.py
Swaroop's address is Swaroopch@byteofpython.info

There are
4 contacts in the address-book

Contact Swaroop at Swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby
-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Contact Guido at guido@python.org
root@sgb
-desktop:~/program# gedit using_dict.py

这个似乎和PHP中的数组很像

4.序列

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

#!/usr/bin/python
#
Filename: seq.py

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[:]
$ python seq.py
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

注:这个挺有意思的,估计以后会经常用到

5.对象与参考

当你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定

#!/usr/bin/python
#
Filename: referance.py

print 'Simple Assignment'
shoplist
= ['apple', 'mango', 'carrot', 'banana']
mylist
= shoplist

del shoplist[0]

print 'shoplist is', shoplist
print 'mylisy is', mylist
#notice that both mylist and shoplist both print the same list without
#
the 'apple' confirming that they point to the same object

print 'Copy by making a full slice'
mylist
= shoplist[:] #make a copy by doing full slice
del mylist[0] #remove first item

print 'shoplist is', shoplist
print 'mylist is', mylist
#notice that now the two lists are deferent
root@sgb-desktop:~/program# python referancee.py
Simple Assignment
shoplist
is ['mango', 'carrot', 'banana']
mylisy
is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist
is ['mango', 'carrot', 'banana']
mylist
is ['carrot', 'banana']

大多数解释已经在程序的注释中了。你需要记住的只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单 对象 ),那么你必须使用切片操作符来取得拷贝。如果你只是想要使用另一个变量名,两个名称都 参考 同一个对象,那么如果你不小心的话,可能会引来各种麻烦。

6.字符串也是对象

#!/usr/bin/python
#
_*_coding: utf-8_*_
#
Filename: str_methods.py

name
= 'Swaroop' #This is a string object

if name.startswith('Swar') :
print 'Yes, the string starts with "Swar"'
if 'a' in name :
print 'Yes, it contains the string "a"'
if name.find('war') != -1 :
print 'Yes, it contains the string "war"'
delimiter
= '_*_'

mylist
= ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist) #join联接
root@sgb-desktop:~/program# python str_methonds.py
Yes, the string starts with "Swar"
Yes, it contains the string
"a"
Yes, it contains the string
"war"
Brazil_
*_Russia_*_India_*_China

这里,我们看到使用了许多字符串方法。startwith方法是用来测试字符串是否以给定字符串开始。in操作符用来检验一个给定字符串是否为另一个字符串的一部分。

find方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串。str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串。

转载于:https://www.cnblogs.com/shaoguobao/archive/2011/05/28/2060902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值