您的位置:首页 > 编程语言 > Python开发

python基本数据类型列表、元组、字典、集合

2019-01-31 19:59 786 查看

文章目录

  • 3.7 基于字典的模糊查找
  • 4、集合
  • 1、列表

    lists = ['a','b','c']
    print(lists)
    
    lists.append('d')
    print(lists)
    print(len(lists))
    
    lists.insert(0,"mm")
    print(lists)
    
    lists.pop()     #pop()删除尾部元素
    print(lists)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    ['a', 'b', 'c']
    ['a', 'b', 'c', 'd']
    4
    ['mm', 'a', 'b', 'c', 'd']
    ['mm', 'a', 'b', 'c']
    
    Process finished with exit code 0

    2、元组

    和列表相比缺点是一旦建之后就无法修改,无法插入删除,只可以访问

    tuples = ('tupleA','tupleB')
    print(tuples[0])
    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    tupleA
    
    Process finished with exit code 0

    3、字典

    key和values可以为任意类型

    3.1 定义字典

    score = {'guanyu':95,'zhangfei':96}
    print(score)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    {'guanyu': 95, 'zhangfei': 96}
    
    Process finished with exit code 0

    3.2 添加

    score['zhaoyun'] = 98
    print(score)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    {'guanyu': 95, 'zhangfei': 96, 'zhaoyun': 98}
    
    Process finished with exit code 0

    3.3 删除

    score.pop('zhangfei')
    print(score)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    {'guanyu': 95, 'zhaoyun': 98}
    
    Process finished with exit code 0

    3.4 查询

    #查看key是否存在
    print('guanyu') in score

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    guanyu
    
    Process finished with exit code 0
    #查询key对应的值
    print(score.get('guanyu'))

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    95
    
    Process finished with exit code 0

    3.5 修改

    #当值不存在时赋一个值为99
    print(score.get('yase',99))

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    99
    
    Process finished with exit code 0

    3.6 遍历

    3.6.1 遍历键和值
    score = {'guanyu':95,'zhangfei':96}
    for name,grade in score.items():
    print(name+':'+str(grade))

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    guanyu:95
    zhangfei:96
    
    Process finished with exit code 0
    3.6.2 只遍历键
    for name in score.keys():
    print(name)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    guanyu
    zhaoyun
    
    Process finished with exit code 0
    3.6.3 只遍历值
    for grade in score.values():
    print(grade)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    95
    98
    
    Process finished with exit code 0

    3.7 基于字典的模糊查找

    模糊查找就是说输入一个key中的一部分,而非整个key,来进行相对应值的查找。

    score = {'guanyu':95,'zhangfei':96,'zhaoyun':80,'songjiang':76}
    while (True):
    n=input("please enter the name of people:")
    for name,grade in score.items():
    if name.startswith(n):
    print(name+':'+str(grade))
    answer=input("wanna quit the game? y/n:")
    if answer=='y':
    break

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    please enter the name of people:g
    guanyu:95
    wanna quit the game? y/n:y
    
    Process finished with exit code 0
    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    please enter the name of people:song
    songjiang:76
    wanna quit the game? y/n:n
    please enter the name of people:zha
    zhangfei:96
    zhaoyun:80
    wanna quit the game? y/n:y
    
    Process finished with exit code 0

    参数介绍:

    startswith(str, beg,end)

    str – 检测的字符串。
    beg – 可选参数用于设置字符串检测的起始位置。
    end – 可选参数用于设置字符串检测的结束位置。

    4、集合

    和字典类似,但是它只有key,没有value,集合中不能包含重复元素,集合是无序的,不支持排序和索引

    s = set(['a', 'b', 'c'])
    s.add('d')
    s.remove('b')
    print(s)
    print('c' in s)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    {'c', 'a', 'd'}
    True
    
    Process finished with exit code 0

    空集合的创建应该用set(),而不是{},因为{}创建出来的默认为一个空字典。

    a=set()
    a.add('m')
    print(a)

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    {'m'}
    
    Process finished with exit code 0

    求交并差集:

    a={1,2,3,4}
    b={3,4,5,6}
    print("a和b的交集为"+str(a&b))
    print("a和b的并集为"+str(a|b))
    print("a和b的差集为"+str(a-b))

    结果:

    C:\Users\Admin\Anaconda3\python.exe D:/winterVocation/GeekTime/day1.py
    a和b的交集为{3, 4}
    a和b的并集为{1, 2, 3, 4, 5, 6}
    a和b的差集为{1, 2}
    
    Process finished with exit code 0
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐