zoukankan      html  css  js  c++  java
  • 列表练习 元组类型 字典类型 小购物车练习

    # 反转 reverse
    # l=['lili','asdf','qwer','80000']
    # l.reverse()
    # print(l) # ['80000', 'qwer', 'asdf', 'lili']

    # 排序 sort
    # l=[1,3,5,7,2,4]
    # l.sort()
    # print(l) # [1, 2, 3, 4, 5, 7] # 将数字按从小到大排列
    # l=[1,3,5,7,2,4]
    # l.sort(reverse=True) # 将数字按照从大到小排列
    # print(l) # [7, 5, 4, 3, 2, 1]
    # x="hello world"
    # y='z'
    # print(x>y) # False # 按照第一个字母的大小来比较
    # x="hello world"
    # y='z2'
    # print(x>y) # False # 数字比字母小
    # l=['lili','asdf','qwer','80000']
    # l.sort()
    # print(l) # ['80000', 'asdf', 'lili', 'qwer']

    # 用列表模拟队列
    # l=[]
    # l.append('qwer')
    # l.append('asdf')
    # l.append('trfg')
    # l.append('poiu')
    # print(l) # ['qwer', 'asdf', 'trfg', 'poiu']
    # print(l.pop(0)) # qwer
    # print(l.pop(0)) # asdf
    # print(l.pop(0)) # trfg
    # print(l.pop(0)) # poiu
    # l=[]
    # l.insert(0,'qwer')
    # l.insert(0,'asdf')
    # l.insert(0,'trfg')
    # l.insert(0,'poiu')
    # print(l) # ['poiu', 'trfg', 'asdf', 'qwer']
    # print(l.pop()) # qwer
    # print(l.pop()) # asdf
    # print(l.pop()) # trfg
    # print(l.pop()) # poiu

    # 用列表模拟堆栈 先进后出 后进先出

    # 元组 tuple 与列表类型相比 只不过[]换成了()对比列表来说 元组不可变 主要是用来读的
    # 列表类型:在中括号【】内存放任意个用逗号隔开的任意类型字符
    # l=['lili','asdf','qwer',80000]
    # l1=('lili','asdf','qwer',80000)
    # print(type(l)) # <class 'list'>
    # print(type(l1)) # <class 'tuple'>
    # l[0]=12
    # print(l) # [12, 'asdf', 'qwer', 80000]
    # l1[0]=12
    # print(l1) # 报错
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # l1[4][0]='c'
    # print(l1) # ('lili', 'asdf', 'qwer', 80000, ['c', 'b'])

    # 按索引取值,正向取反向取,只能取
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # print(l1[0]) # lili

    # 切片 顾头不顾尾 步长
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # print(l1[0:3]) # ('lili', 'asdf', 'qwer')
    # print(l1) # ('lili', 'asdf', 'qwer', 80000, ['a', 'b'])

    # 长度
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # print(len(l1)) # 5

    # 成员运算 in 和 not in
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # print(80000 in l1) # True

    # 循环
    # l1=('lili','asdf','qwer',80000,['a','b'])
    # for item in l1:
    # print(item) # lili asdf qwer 80000 ['a', 'b']

    # 掌握
    l1=('lili','asdf','qwer',80000,['a','b'])
    # print(l1.index(80000)) # 3
    # print(l1.index(800000)) # 报错
    # print(l1.count(80000)) # 1

    # 字典 key:value 存放多个值 存取速度快
    # key必须是不可变类型(int,float,str,tuple) value可以是任意类型
    # info={'name':'OBOS','age':19,'gender':'female'} # info=dict{'name':'OBOS','age':19,'gender':'female'}
    # info=dict(name='OBOS',age=19,gender='female')
    # print(info) # {'name': 'OBOS', 'age': 19, 'gender': 'female'}
    # info1=dict([('name','OBOS'),('age',19),('gender','female')])
    # info2=dict([['name','OBOS'],['age',19],['gender','female']])
    # info3=dict([['name','OBOS'],('age',19),['gender','female']])
    # print(info1) # {'name': 'OBOS', 'age': 19, 'gender': 'female'}
    # print(info2) # {'name': 'OBOS', 'age': 19, 'gender': 'female'}
    # print(info3) # {'name': 'OBOS', 'age': 19, 'gender': 'female'}
    # 快速创建一个空字典 # fromkeys
    # info={}.fromkeys(['name','age','gender'],None)
    # print(info) # {'name': None, 'age': None, 'gender': None}
    # info2={}.fromkeys('hello',None)
    # print(info2) # {'h': None, 'e': None, 'l': None, 'o': None} k不能重复
    # 按照k存取值,可存可取
    # d={'name':'OBOS'}
    # print(d['name']) # OBOS
    # d['age']=19 # **************
    # print(d) # {'name': 'OBOS', 'age': 19}
    # 长度
    # info={'name':'OBOS','age':19,'gender':'female'}
    # print(len(info)) # 3
    # 成员运算 in 和 not in 根据字典的Key判断
    # info={'name':'OBOS','age':19,'gender':'female'}
    # print('name' in info) # True
    # 删除
    # info={'name':'OBOS','age':19,'gender':'female'}
    # ################################info.pop('name')
    # print(info.pop('name')) # OBOS
    # print(info) # {'age': 19, 'gender': 'female'}
    # info={'name':'OBOS','age':19,'gender':'female'}
    # print(info.popitem()) #('gender', 'female') # 元组的形式
    # print(info) # {'name': 'OBOS', 'age': 19}

    #keys values items(键值对)
    # info={'name':'OBOS','age':19,'gender':'female'}
    # print(info.keys()) # dict_keys(['name', 'age', 'gender']) # 不是一个列表但是可以被for循环,for循环不靠索引
    # print(info.keys(0)) # 报错
    # print(list(info.keys())) # ['name', 'age', 'gender'] # 此时是一个列表
    # print(list(info.keys())[0]) #name
    # print(info.values()) # dict_values(['OBOS', 19, 'female'])
    # print(list(info.values())) # ['OBOS', 19, 'female']
    # print(info.items()) # dict_items([('name', 'OBOS'), ('age', 19), ('gender', 'female')])
    # print(list(info.items())) # [('name', 'OBOS'), ('age', 19), ('gender', 'female')]
    # info={'name':'OBOS','age':19,'gender':'female'}
    # for k in info:
    # # print(k) # name age gender
    # print(k,info[k]) # name OBOS # age 19 # gender female #
    # *********************************小购物车********************************************
    # msg_dic={'apple':10, 'tesla':100000,'mac':3000, 'beef':48, 'US beef':68,'Tuekey leg':8}
    # goods=[]
    # while True:
    # for k in msg_dic:
    # print(k,msg_dic[k])
    # choice=input('商品名:').strip()
    # if len(choice) == 0 or choice not in msg_dic:
    # print('商品名非法')
    # continue
    # while True:
    # num=input('购买个数:').strip()
    # if num.isdigit():
    # break
    # goods.append((choice,msg_dic[choice],int(num)))
    # print('购物车',goods)

    # 字典类型
    # info={'name':'OBOS','age':19,'gender':'female'}
    # print(info['love']) # 报错
    # print(info.get('love','没有')) # 没有
    # print(info.pop('love')) # 报错
    # print(info.pop('love','有')) # 有
    # d={'name':'Alex','love':'120'}
    # info.update(d)
    # print(info) # {'name': 'Alex', 'age': 19, 'gender': 'female', 'love': '120'}

    # setdefault ************************
    # info={'name':'OBOS','gender':'female'}
    # info.setdefault('age',19)
    # print(info) # {'name': 'OBOS', 'gender': 'female', 'age': 19}
    # value=info.setdefault('age',19)
    # print(value) # 19
    # info={'name':'OBOS','age':19,'gender':'female'}
    # info.setdefault('age',18)
    # print(info) # {'name': 'OBOS', 'age': 19, 'gender': 'female'}
    # value=info.setdefault('age',18)
    # print(value) # 19 如果key存在,则不修改,返回已经存在的key的value值**************
    # *********************************************************************************************************************
    # info={'name':'OBOS','age':19,'gender':'female'}
    # info['hobbies']=[]
    # # print(info) # {'name': 'OBOS', 'age': 19, 'gender': 'female', 'hobbies': []}
    # info['hobbies'].append('music')
    # info['hobbies'].append('dance')
    # print(info) # {'name': 'OBOS', 'age': 19, 'gender': 'female', 'hobbies': ['music', 'dance']}
    # info={'name': 'OBOS', 'age': 19, 'gender': 'female', 'hobbies': ['music', 'dance']}
    # info['age']=20
    # print(info) # {'name': 'OBOS', 'age': 20, 'gender': 'female', 'hobbies': ['music', 'dance']}
    # info = {'name': 'OBOS', 'age': 20, 'gender': 'female', 'hobbies': ['music', 'dance']}
    # hobbies_list=info.setdefault('hobbies',[])
    # print(hobbies_list) # ['music', 'dance']
    # hobbies_list.append('read')
    # print(info) # {'name': 'OBOS', 'age': 20, 'gender': 'female', 'hobbies': ['music', 'dance', 'read']}
    # info = {'name': 'OBOS', 'age': 20, 'gender': 'female'}
    # hobbies_list=info.setdefault('hobbies',[])
    # print(hobbies_list)
    # hobbies_list.append('read')
    # print(info) # {'name': 'OBOS', 'age': 20, 'gender': 'female', 'hobbies': ['read']}
    # nums=[11,22,33,44,55,66,77,88,99]
    # d={'k1':[],'k2':[]}
    # for num in nums:
    # if num > 66:
    # d['k1'].append(num)
    # if num < 66:
    # d['k2'].append(num)
    # print(d) # {'k1': [77, 88, 99], 'k2': [11, 22, 33, 44, 55]}
    cmd='Rambo Alex Rambo Sean Alex Rambo'
    words=cmd.split()
    # print(words) # ['Rambo', 'Alex', 'Rambo', 'Sean', 'Alex', 'Rambo']
    d={}
    for word in words:
    d.setdefault(word,cmd.count(word))
    print(d)
  • 相关阅读:
    Linux .o a .so .la .lo的区别
    linux源码Makefile详解
    Kconfig详解
    如何将驱动程序静态编译进内核
    getpeername
    Socket programming in C on Linux | tutorial
    C Socket Programming for Linux with a Server and Client Example Code
    UDP protocol
    TCP protocol
    How to learn linux device driver
  • 原文地址:https://www.cnblogs.com/0B0S/p/11914070.html
Copyright © 2011-2022 走看看