有这样的字符串:a=‘helloworld’ 统计字符串中字符出现的次数
from collections import Counter
a='hello'
print(Counter(a))
d={‘a’:10,‘F’:20,‘D’:‘B2’,‘D’:‘555’,‘e’:‘123’}字典中key值是小写字母的键值对
d={'a':10,'F':20,'D':'B2','D':'555','e':'123'}
for key,value in d.items():
if key.islower()==True :
dict.update({key: value})
print(key,value)
d={‘a’:10,‘F’:20,‘D’:‘B2’,‘D’:‘555’,‘e’:‘123’}字典中所有的key变为小写 结果:
d={'a':10,'F':20,'D':'B2','D':'555','e':'123'}
dc = {}
for key,value in d.items():
dc[key.lower()]=value
print(dc)
将d={‘a’:10,‘F’:20,‘D’:‘B2’,‘D’:‘555’,‘e’:‘123’}字典中所有的的value统一赋值为’error’ 结果:
d={'a':10,'F':20,'D':'B2','D':'555','e':'123'}
for key in d:
d[key]='error'
print(d)
cookies="JSESSIONID=abcMktGLRGjLtdhBk7OVw;first_login_flag=1;ln_uact=55883008898;wp_fold=0"字符串转换成字典的形式 结果
cookies="JSESSIONID=abcMktGLRGjLtdhBk7OVw;first_login_flag=1;ln_uact=55883008898;wp_fold=0"
dc = {cookies.split('=')[0]:cookies.split('=')[1] for cookies in cookies.split(';')}
print(dc)
第一种写法
s='username'
print(s.replace(s,'UserName'))
第二种写法
s='username'
print(s[0:4].capitalize()+s[4:].capitalize())
第三种写法
s='username'
s1=''
for i in s:
if i=='u' or i=='n':
s1+=i.upper()
else:
s1+=i
print(s1)
sum_1=[]
sum_2=[]
for i in range(101):
if i%2==0:
sum_2.append(i)
else:
sum_1.append(i)
print(sum_1)
print(sum_2)
l=[]
for i in range(8):
if i==0 or i==1:
l.append(1)
else:
l.append(l[i-2]+l[i-1])
print(l)
1=[1,1,4,2,2,5,6,9]实现把元素去重后返回一个新的列表结果是:
[1,1,4,2,2,5,6,9]
l1=[]
for i in l:
if i not in l1:
l1.append(i)
print(l1)
l=['a','b','c']
d1={}
for i in l:
d1[i]='None'
print(d1)
l=[‘abc’,‘bb1’,‘c3’] l1=[1,2,3]
把这两个列表转换为字典结果
l=['a','b','c']
l1=[1,2,3]
d={}
c = dict(zip(l,l1))
print(c)
a='I LOVE YOU'
ab=a.replace('YOU','2023')
print(ab)
a='A1:1|A2:2|A3:3'
d={}
d1=a.split('|')
for i in d1 :
s1 = i.split(':')
d.setdefault(s1[0],s1[1])
print(d)
for x in range(1,10):
for y in range(1,x+1):
print('%d*%d=%d' % (x,y,x*y))
l = [4, 2, 1, 3, 4, 2, 5, 9, 3, 2, 2, 2,9,9,9],去除列表中的重复元素
第一种
l = [4, 2, 1, 3, 4, 2, 5, 9, 3, 2, 2, 2,9,9,9]
b1 = set(l)
print(b1)
第二种:
l = [4, 2, 1, 3, 4, 2, 5, 9, 3, 2, 2, 2,9,9,9]
l1 = []
for i in l:
if i not in l1:
l1.append(i)
print(l1)
def prints(n):
if (n > 0):
prints(n-1)
print(n)
if __name__ == '__main__':
prints(10)
def a1():
for i in range(1,11):
print(i)
if __name__ == '__main__':
a1()
a='ABC'
a2 = sorted(a,reverse=True)
print(a)
a3 = ''.join(a2)
print(a3)
l1=['wang','li','wt','kkke']
l2=[10,20,30,40]
l3= dict(zip(l1,l2))
print(l3)
第二种:
l1=['wang','li','wt','kkke']
l2=[10,20,30,40]
l3={}
for i in range(len(l1)):
l3[l1[i]]=l2[i]
print(l3)
from collections import Counter
s='I love abc'
print(Counter(s))
import operator;
d={"stu1":50, "stu2":20, "stu3":30}
d1=sorted(d.items(),key=operator.itemgetter(1),reverse=True)
print(d1)
l =[1,2,3,5,6]
l1 = [str(i) for i in l]
l2 = ''.join(l1)
print(l2)
s = “256{}{}{111{555}}{{}}{}{}”
s1 = "{}{}"
if s.count(s1)!=0:
print('yes')
else:
print('no')
a=‘aaabbbccckdkdkdeitrugalgfklafdcv,zcv.,mhkaghf’
def first_char(str):
d = {}
for i in range(len(str)):
if str[i] in d:
d[str[i]] += 1
else:
d[str[i]] = 1
for i in range(len(str)):
if d[str[i]] == 1:
return '第一个不重复的字符串是{},索引是{}'.format(str[i], i)
return "None"
if __name__ == '__main__':
s1 = 'aaabbbccckdkdkdeitrugalgfklafdcv,zcv.,mhkaghf'
res = first_char(s1)
print(res)
l = [10,5,1,2,3,4,5,6,7,8,9]
def mu(list):
l1 =[]
for i in range(1,len(list)):
j = i-1
mut = list[i]*list[j]
l1.append((mut,list[i],list[j]))
return max(l1)
mut,left,right = mu(l)
print(mut,left,right)
def sum(n):
j = 0
for i in range(0,n):
j=j+i
print(j)
sum(5)
有一查询余额的系统,帐号:user1 密码:123456 验证码:1234
登录成功,查询您的余额是:您的余额是10万元
输入错误,返回请重新输入
a = input('请输入账号')
if a =='user1':
b = input('请输入密码')
if b =='123456':
c = input('请输入验证码')
if c=='1234':
print('您的余额是10万元')
else:
print('输入错误,返回请重新输入')
url=“http://www.abc.com?page_size=30&page_index=31&user_id=2023”
def funadd():
url="http://www.abc.com?page_size=30&page_index=31&user_id=2023"
sp=url.split('?')
del sp[0]
d={}
for i in sp:
s1=i.split('&')
for j in s1:
s2=j.split('=')
d.setdefault(s2[0],s2[1])
print(d)
funadd()
def d(n):
a =1
for i in range(1,n):
a=a*i
print('%d!=%d'%(i,a))
d(11)
第二种:
import math
def d(n):
print('%d!=%d'%(n-1,math.factorial(n)))
d(11)
data ={'root1':'123456','admin':'1234'}
def login():
username=input('请输入账号')
if not data.__contains__(username):
print('请先注册')
reg()
else:
userpwd=input('输入密码')
if userpwd==data[username]:
print('登录成功')
else:
print('密码错误')
def register():
zcname=input('请输入账号:')
if data.__contains__(zcname):
print('账号已经注册')
logjump()
else:
rgpwd1=input('输入密码:')
rgpwd2=input('再次确认密码:')
if rgpwd1==rgpwd2:
print('两次的密码一致,注册成功')
data.setdefault(zcname,rgpwd2)
print(data)
logjump()
else:
print('密码不一致,重新注册')
register()
if __name__ == "__main__":
login()