python学习5

1.输入一个字符串,从中计算有几个整数

info = input('》》》》')
for i in info:
    if i.isalpha():
        info = info.replace(i,' ')
l = info.split()
print(l)
print(len(l))


# a = []
# j = -1
# b = 0
# k = 0
# c = []
# cont = 0
# content = input('请随便输入点东西,最好有数字')
# for i in content:
# j = j + 1
# if i.isdigit():
# a.append(j)
# b = len(a)
# while k <= b-2:
# if a[k+1] != a[k] + 1:
# cont = cont + 1
# print(k)
# k += 1
# cont = cont + 1
# print(a)
# print(cont)

2.作业

# li = ['alex','wusir','eric','rain','alex']
# length = len(li)
# print('1长度是'+str(length))
# li.append('seven')
# print('2新列表是' + str(li))
# li.insert(0,'Tony')
# print(li)
# li[1] = 'kelly'
# print(li)
# l2 = [1,'a',3,4,'hert']
# li.extend(l2)
# print(li)
# li.extend('qwert')
# print(li)
# li.remove('eric')
# print(li)
# li.pop(1)
# #
# li.reverse()
# print(li)

# num = li.count('alex')
# print(num)

# li = [1,3,2,'a',4,'b',5,'c']
# l1 = li[0:3]
# print(l1)
# l2 = li[3:6]
# print(l2)
# l3 = li[0:-1:2]
# print(l3)
# l4 = li[1:-2:2]
# print(l4)
# l5 = list(li[-1])
# print(l5,type(l5))
# l6 = li[-3:0:-2]
# print(l6)

#3
lis = [2,3,'k',['qwe',20,['k1',['tt',3,1]],89],'ab','adv']
# lis[3][2][1][0] = lis[3][2][1][0].upper()
# print(lis)
# lis[3][2][1][0] = lis[3][2][1][0].replace('tt','TT')
# print(lis)
lis[1] = '100'
lis[3][2][1][1] = '100'
print(lis)
View Code

3.字典的增删改查

增加: 直接用键值加,用dic.setdefault('key','value')

dic1 = {'age' : 18, 'name' : 'jin', 'sex' : 'male'}
# # #增:
# dic1['height'] =  185  #没有键值对,添加
# print(dic1)
# dic1['age'] = 16         #有键值对,则值覆盖
# print(dic1)
# dic1.setdefault('weight')
# dic1.setdefault('weight',150)   #没有键值对,增加
# print(dic1)
# dic1.setdefault('name','二哥')  #有键值对,不做改变

删除: dic.pop('name','none'),dic.popitem(),dic.clear(),del

#删除   pop  按键删除,有返回值
# print(dic1.pop('age'))
# print(dic1)
# dic1.pop('ed',None)   #不知道有没有错的情况下,删除,可设置返回值
# print(dic1.pop('edd','没有此键'))


# dic1 = {'age' : 18, 'name' : 'jin', 'sex' : 'male'}
# print(dic1.popitem())   #随机删除,3.6删除最后一个,返回删除的键值
# print(dic1)

# dic1.clear()  #清空
# print(dic1)

# del dic1
# print(dic1)

改:dic.update(dic2)    用键值当索引来改

#
# dic = {'name':'jin','age':18,'sex':'male'}
# dic2 = {'name':'alex','weight':75}
# dic2.update(dic)        #把dic的所有键值对更新到dic2中,没有的加入,有的覆盖
# print(dic)
# print(dic2)

查:用for循环来遍历,字典的dic1.keys(),dic1.values(),dic1.items()--->变成了由(key,value)元组的列表

      dic1.get('key','Information')   查询键值key,查询到则返回value值,没有查询到相应的key则返回信息Information

dic1 = {'age':18,'name':'jin','sex':'male'}
# print(dic1.keys(),type(dic1.keys()))     #就是一个列表,得出键值的列表
# print(dic1.values())   #value值的列表
# print(dic1.items())    #dict_items([('age', 18), ('name', 'jin'), ('sex', 'male')])列表里面是元组,元组里面是键值
# for i in dic1:   #键
#     print(i)
# for i in dic1.keys():
#     print(i)
# for i in dic1.values():
#     print(i)

print(dic1.items())

for k,v in dic1.items():
    print(k,v)
# ('age', 18)
# ('name', 'jin')
# ('sex', 'male')
# age 18
# name jin
# sex male
#get
v1 = dic1['name']
print(v1)
#没有相应的键就会报错
print(dic1.get('name1','没有这个键'))
 
 

4.字典的嵌套

dic = {
    'name':['alex','wusir','taibai'],
    'python':{
        'time':'121314',
        'money':'free',
        'address':'CBD',
               },
    'age':21
        }
dic['age'] = 56
print(dic)
dic['name'].append('xddd')
print(dic)
dic['name'][1] = dic['name'][1].upper()
print(dic)
dic['python']['female'] = 6
print(dic)
dic['python'].setdefault('male',15)
print(dic)

 

转载于:https://www.cnblogs.com/benbenxiaofeifei/p/9320677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值