Python_基本数据结构示例

来源:互联网 发布:cf手游刷枪软件绿色版 编辑:程序博客网 时间:2024/04/29 08:54
#if条件语句x=int (input("please input an integer:"))if(x<60):    print("Not pass")elif(x<70):    print("you got D")elif(x<80):    print("you got C")elif(x<90):    print("you got B")else:    print("you got A")


#for循环语句for i in range(1,10):    for j in range(1,i+1):        print('%s*%s=%s\t'%(j,i,i*j),end='')    print()


#排序>>> s1=[1,3,2,5,4]>>> sorted(s1)[1, 2, 3, 4, 5]>>> reversed(s1)<list_reverseiterator object at 0x0000000002562780>>>> s1.sort()>>> s1[1, 2, 3, 4, 5]>>> s1.reverse()>>> s1[5, 4, 3, 2, 1]
>>> a=[1.0,2.0]>>> b=[1,2]>>> a==bTrue>>> c=(1,2)>>> d=(1,2,-1)>>> c<dTrue


#列表list>>> s1=list(map(lambda x:x**2,range(10)))  #关于lambda函数http://blog.csdn.net/hpu_a/article/details/50244687>>> s1[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]>>> s2=[x**2 for x in range(10)]>>> s2[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]>>> s3=[(x,y) for x in range(10) for y in r>>> s3[(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)]


#集合set>>> a = set('abcagd')>>> b = set('abcdef')>>> a{'a', 'c', 'g', 'd', 'b'}>>> b{'a', 'c', 'f', 'b', 'd', 'e'}>>> a - b{'g'}>>> a | b{'a', 'g', 'c', 'f', 'b', 'd', 'e'}>>> a & b{'a', 'c', 'd', 'b'}>>> a ^ b{'g', 'e', 'f'}>>> {x for x in a if x not in b}{'g'}

#字典dict>>> dic={'one':1,'two':2,'three':3}>>> dic{'one': 1, 'three': 3, 'two': 2}>>> dic('four')=4  File "<stdin>", line 1SyntaxError: can't assign to function call>>> dic['four']=4>>> dic{'one': 1, 'three': 3, 'two': 2, 'four': 4}>>> list(dic.keys())['one', 'three', 'two', 'four']>>> sorted(dic.keys())['four', 'one', 'three', 'two']>>> {x : x**2 for x in range(5)}{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> l=[1,2,3,4,5,6]>>> a=dict([(v,v*10) for v in l])>>> a{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
for x,y in dic.items():    print(x,y)two 2four 4three 3one 1



























0 0
原创粉丝点击