#!/usr/local/bin/python
#encoding:utf-8
'''
小练习 6
遍历一个序列[1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]
求这个list的最大的值
'''
# arr = [1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]
# Max = 0
# for i in arr:
# if i>Max:
# Max=i
# print Max
#小练习 7
#用户输入数字 判断是不是闰年
#如果是100的倍数,要被400整除
#被4整除
#比如1900不是闰年,2000,2004是闰年
#如果输入不是闰年,提示信息,并且继续输入
# x = int(raw_input('请输入一个年份 '))
# while not ((x%100==0 and x/400>0) or (x%4==0 and x%100!=0)):
# x = int(raw_input('您输入的不是闰年,请重新尝试一个年份: '))
# print '您输入是闰年'
# while True:
# num = int(raw_input('please input a number : '))
# if (num%4==0 and num%100!=0) or num%400==0:
# print 'Yes'
# break
# print 'No'
'''
list是有顺序的[]
dict是没有顺序的{}
'''
# d = {'name':'chris','age':20}
# print d['name'] # 根据key获取value
# d['age'] = 50 # 修改值
# d['newkey'] = 'newval' # 新增
# print d
'''
小练习 8
['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node']
求出这个list中,每个字符出现的次数
'''
# arr = ['C','js','python','js','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node','css','js','html','node','js','python','js','css','js','html','node','js','python','js','css','js','html','node']
# d = {}
# for i in arr:
# if i not in d:
# d[i]=1
# else:
# d[i]+=1
# print d
'''
Linux下Python脚本执行方式./xx.py
'''
# arr = ['zxc',1,False]
# print False in arr
'''
小练习 01
实现 in
'''
# arr = ['zxc','fadf',145,632]
# s = raw_input('input a string: ')
# is_in_arr = False
# for i in arr:
# if i==s:
# is_in_arr = True
# else:
# pass
# print is_in_arr
'''
***切片可以赋值
'''
# arr = ['c','python','js','css','php','html']
# arr[2:4]=['Alvin','Sherman']
#指定位置插入元素
# arr[1:1]=['Alvin','Sherman']
#指定位置删除元素
# arr[2:4]=[]
#实现append功能
# arr[len(arr):]=[2,]
# print arr
'''
实现一个list排序
'''
# arr = [4111,22,3333,444,111,4,5,777,65555,45,33,45]
# for i in range(len(arr)-1):
# for j in range(len(arr)-i-1):
# if arr[j+1]<arr[j]:
# arr[j+1],arr[j]=arr[j],arr[j+1]
# else:
# pass
# print arr
'''
count统计list中某个元素的出现次数
'''
# arr = [1,2,6,4,5,7,3,2,4,1,2,3,1,3,2,1]
# print arr.count(1)
'''
小练习 03
实现fromkeys的功能
'''
# a={}.fromkeys(['age','name'])
# b={}.fromkeys(['age','name'],['wd','hgf0'])
# def fkey(list,k=None):
# dict={}
# for i in list:
# dict[i]=k
# return dict
# z=fkey(['age','name'],['wd','hgf0'])
# print z
'''
get
更宽松的通过key访问value的方式,key不存在不会报错,可以提供默认值
'''
# d = {}
# # print d['name']
# print d.get('name')
# print d.get('name','chenhao')
# d['name']='Alvin'
# print d.get('name','chenhao')
'''
has_key
has_key与in的功能一样
'''
# d={}
# print d.has_key('name')
'''
items将字典所有项以列表的形式返回,每一项是(key, value)
'''
#
# d={'name':'wd','age':20}
# print d.items()
# print dict(d.items())
'''
keys字典中的键,以列表的形式返回
'''
# d={'name':'wd','age':20}
# print d.keys()
'''
popitem类似于list.pop,弹出一个随机的项
'''
# d = {'name':'wd','age':12}
# print d.popitem()
# print d
'''
setdefault类似于get,可以赋默认值
当字典中没有改key值时,setdefault可以赋值并返回相应赋予的值
注意当字典中该key对应值为None时,setdefault不会进行赋值,因为存在key值
'''
# d = {'name':'Chris'}
# d = {'name':None}
# print d.setdefault('name','pc')
# print d
'''
update 可以用一个字典去更新另一个字典
存在相应key值即更新,不存在即新增
'''
# d = {'name':'Chris'}
# d1 = {'name':'Alvin'}
# d.update(d1)
# print d
'''
小练习 05 实现update功能
'''
'''
小练习 06 把一个dict的key和value反转
如果value有重复的,把多个key放在list
提示:判断是否list
print isinstance([1,2,3],list)
print type([1,2])==type([])
比如{'teach':'pc','waihao':'pc','name':'pc','age':12,'job':'IT'}
期望输出{'pc':['teach','waihao','name'],12:'age','IT':'job'}
'''
# dict1 = {'teach':'pc','waihao':'pc','name':'pc','age':12,'job':'IT'}
# dict2 = {}
# for key in dict1:
# if dict1[key] not in dict2:
# dict2[dict1[key]]=key
# else:
# if isinstance(dict2[dict1[key]],list):
# dict2[dict1[key]].append(key)
# else:
# dict2[dict1[key]]=[dict2[dict1[key]],key]
# print dict2
#方法2
# def rev(dic):
# newDic = {}
# for key,val in dic.items():
# if val not in newDic:
# newDic[val]=key
# else:
# if isinstance(newDic[val],list):
# newDic[val].append(key)
# else:
# newDic[val] = [newDic[val],key]
# return newDic
#
# def rev2(dic):
# newDic = {}
# for key,val in dic.items():
# if isinstance(val,list):
# for i in val:
# newDic[i] = key
# else:
# newDic[val] = key
# return newDic
#
# d = {'teach':'pc','waihao':'pc','name':'pc','age':12,'job':'IT'}
# print rev(d)
# print rev2(rev(d))
'''
字符串
'''
# str = 'hello'
# print str[0]
'''
字符串的拼接
join
'''
# print '|'.join(['hello','world'])
'''
字符串 find
find(a)检测str是否包含a
检测是否包含在指定的范围内,如果在则返回开始的索引值,否则返回-1
'''
# print 'helloworld'.find('e')
# print 'helloworld'.find('s')
# print 'helloworld'.find('o',5)
'''
字符串 index
index用法和find一样,但是不存在的时候会报错
'''
# print 'helloworld'.index('e')
# print 'helloworld'.index('s')
'''
字符串
capitalize upper lower
'''
# print 'hello'.capitalize()
# print 'hello'.upper()
# print 'hello'.lower()
'''
小练习 09
用户输入员工姓名和ID,名字和ID之间用":"分隔,多个用户之间用","分割
最终输入所有用户对应ID的信息
比如用户输入user1:112,user2:119,user3:114
最终输出[('user1',112),('user2',119),('user3',114)]
'''
# users = raw_input('please input username and id :')
# users = 'user1:112,user2:119,user3:114'
# splitUsers=users.split(',')
# dic = {}
# for i in range(len(splitUsers)):
# lst = splitUsers[i].split(':')
# dic[lst[0]] = lst[1]
# print dic.items()
#方法2
#a = raw_input('please input username and id :')
a = 'user1:112,user2:119,user3:114'
s1 = []
for i in a.split(','):
s1.append(tuple(i.split(':')))
print s1
'''
作业:第二题统计字符数量之后,打印出次数前10的字符
'''
Python 基础练习02
最新推荐文章于 2022-06-11 20:51:44 发布