您的位置:首页 > 理论基础 > 数据结构算法

在.NET外散步之我爱贪吃蛇Python -常见数据结构(新浪和百度云平台即将推出Python免费空间)

2012-06-01 21:45 495 查看
新浪的http://cloudbbs.org/forum.php?mod=viewthread&tid=2762&extra=page%3D1


百度的http://tieba.baidu.com/p/1605220585


Python常见的数据类型有以下这么几种


Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange) !!!!


刚开始接触时,总是在类比这个数据结构是c#里面的什么呢或者向c#里面的什么呢,左边折腾一下右边折腾一下,发现Python的数据结构和c#里面的数据结构没有什么相似的地方,


想借鉴一下c#的概念理解一下也不行,Python里面竟然连数组这个概念都没有,是在令人非常费解,开始还以为没有找到数组,后来发现结构体也没有,偷鸡不成。。。。


序列

Python里面最基础的数据结构为序列,序列主要有两个常用操作就是索引操作和切边操作,非常灵活,列表、元组、字符串都是序列


索引操作和切边操作都按照下面这个编号进行

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0   1   2   3   4   5
-5  -4  -3  -2  -1


 


 


 


# -*- coding: utf8 -*-


[code] 


if __name__ == '__main__':


shoplist3=["a","b","c","d","e",1,2,3,4,5,6,7]


#序列相关


print(shoplist3)


print(len( shoplist3))




#按照索引操作


print(shoplist3[3])


shoplist3[3]="dddddddddddddddddddddddd"


print(shoplist3[3])




#按照切片操作


print(shoplist3[:])


print(shoplist3[1:3])


print(shoplist3[:-1])


print(shoplist3[-3:-1])


print(shoplist3[2:-1])


print(shoplist3[3][0])




#字符串作为序列操作


print("=====================")


helpa="helpa"


print(helpa[1:3])


print(helpa[-4:-2])


print("=====================")




#序列作为栈


shoplist3.append("xxxxxxxxxx")


print(shoplist3)


shoplist3.pop()


print(shoplist3)




print("=====================")


#序列作为队列


from collections import deque


queue = deque(["Eric", "John", "Michael"])


#deque 表示费解改个名字就不对了


queue.append("Terry")


queue.append("Graham")


print(queue)


queue.popleft()


print(queue)



[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 


序列本身可以作为栈来操作,还可以转化为队列进行操作


元组

      开始觉得Python没有常量这个概念太离谱了,遇到元组,发现元组就是天生的常量类型,元组是也属于序列,支持索引和切片


zoo=("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa","b","c","d",1,2,3,4)


[code]print(zoo)


print(len(zoo))


print(zoo[4])


#zoo[4]=100000   


#果然是不可以改变值 也不可以改变大小,


#可用来模拟 常量 


print(zoo[0])


print(zoo[0][4])

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Sets

   Sets 也是Python的一种数据类型而且支持一些运算


 a = set('abracadabra')


[code]print(a)


print("a" in a)


b = set('alacazam')




print(a - b )


# letters in a but not in b


print(a | b )


# letters in either a or b


print(a & b )


# letters in both a and b


print(a  ^ b )


# letters in a or b but not both

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

字典

Python中和c# 中最相似的就是字典这个概念操作也差不多

#字典


[code]hashtable={


   "a":"aaaaaaaaaaaaaaaaaaa",


   "b":"bbbbbbbbbbbbbbbbbbbb",


   "c":"ccccccccccccccccccccc",


   "d":"dddddddddddddddddddddd",


   1:2,


   "e":"eeeeeeeeeeeeeeeeeeeeeeeeee"


   


   }


print(hashtable)


hashtable["f"]="fffffffffffffffffffffffffffffff"


print(hashtable)


#不支持通过坐标访问 只支持通过key来访问


#print(hashtable[0])


print(hashtable["a"])  


for key, value in hashtable.items():


print(key)


print(value)


#help(hashtable)

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

另外还有一个声称序列的方法range 可以生成指定范围内的序列

 print("===========================")


[code]print(range(10))#范围0~9


print(range(3,13))#范围3~12 range([start], stop[, step])


print(range(0,30,5))#步长5


print(range(0,10,3))#步长5


print("===========================") 


#print(xrange(0,10)) Y的怎么没反应呢?????


print(u"汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字")

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: