Python基础数据结构

其他东西自己学,数据结构和算法是精髓,语法什么的书上都有,直接敲代码体验基本数据结构用法
下面上代码咯,用到函数的地方会有英文批注

print r'''
she is 
the shine
**       ***
   *   *   ***
     *      ***
  LA & YRC    **
            *** 
           ***
          ***
         **
     *** 
nice
Let's Start
'''
print 'About List and Tuple'
L=['Iverson',100,'Kobe',98,'Nicholas',100,'Bryen',50]
print L
print L[1]
print L[-1]# -1 means the last one in List, -0 doesn't work
L.append('Paul')#insert element at the end
L.insert(0,'Dad')#insert element by index and value
print L
L.pop(0)#delete element by index
print L
L.pop()#delete the last element
L[-1]=89#set the last element's value
print L
#t means tuple : List witch could not be modified
t = (0,1,2,3,4,5,6,7,8,9)
print t
print ('*')*80
print 'About ifelse and forwhile'
score = 75
if score >= 60:
    print 'passed'
score = 55 #pay more attention about ':' after if or else
if score >= 60:
    print 'passed'
else:
    print 'failed'
for name in L:
	print name
s = [75, 92, 59, 68]
sum = 0.0
for score in s:
    sum=sum+score
print sum / 4
sum = 0
x = 1
n = 1
while True: #while True equals while(0)in C or C++ more practice
    sum=sum+x
    x=2*(n-1)
    n=n+1
    if n>20:
        break;
print sum
print n
for x in [ 1,2,3,4,5,6,7,8,9]:#more practice
    for y in [ 0,1,2,3,4,5,6,7,8,9 ]:
        if x<y:
            print x*10+y
			
print ('*')*80
print 'About Dict and Set'
#dict an assemble like key-value
d = {
    'Adam': 95,
    'Lisa': 85,
    'Bart': 59,
    'Paul': 75
}
n=len(d)#function len()
print d
print n
print "Adam"+":"+str(d.get('Adam'))#str() change int to string
d['Dad']=100#add a new key-value into a dict
for key in d:#print every key with its value 
    print key+":"+str(d.get(key))
print d
d = {
    'Adam': 95,
    'Lisa': 85,
    'Bart': 59
}
print d.get('Adam')#function to get value by key
print d['Lisa']# array style to get value by key
if 'Paul' in d:#more practice
    print d.get('Paul')
else:
    print "no Paul in d"

s = set(['Adam','Lisa','Bart','Paul'])
print s
l=['Adam','Lisa','Bart']# suggest  initialing a set by this way:create a List first,then set the list
s = set(l)
print s

l=['January','February','March','April','June','July','Augest','September','October','November','December','Augest']
months = set(l)
print l#list got two value named 'Augest'
print months#set got only one value named 'Augest'
x1 = 'February'
x2 = 'Sun'

if x1 in months:
    print 'x1: ok'
else:
    print 'x1: error'

if x2 in months:
    print 'x2: ok'
else:
    print 'x2: error'
	
l=[('Adam', 95), ('Lisa', 85), ('Bart', 59)]#list can be created as a key-value style
print l
s = set(l)
for x in s:
    print x
for x in l:
    print x
	
s = set(['Adam', 'Lisa', 'Paul'])#use function add or remove to reset a set
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for key in L:
    if key in s:
        s.remove(key)
    else:
        s.add(key)
print s

上面都是自己敲的,全部复制保存为py文件就可以用了,放在这里完全是为了自己稳固而知新,重要的地方都用#批注了一波
下面说一下目前我所学到的核心数据结构如下:List、Turple、Dict和Set,目前我所感知的就List和Dict,List就是一个类似数组的东西,有序的,每个元素的东西居然可以使key-value模式的(要dict来干嘛),但是是有序的,然后通过append(value)insert(position,value)函数可以增加元素,通过pop(positon)可以删除元素;dict是一个标准的map容器,专门存放key-value这种键值模型,无序的,通过update(key=vlaue)增加成员,pop(key)删除成员;
另外,List元素查询速度随List长度增加而减慢,dict不存在,速度都一样,但是浪费空间,典型的空间换时间
关于turple 和set 感觉用的地方目前为止我不涉及,我弟弟做毕设那天估计也可以不用,所以就不废话了。

胖娃儿加油 LA

好吧 代码很凌乱 没有分门别类 后续抽时间按照每个数据结构类型增删改查功能分别总结并举例说明 顺便自己温习一波

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值