def fun_dict(list):
dt = {}
for item in list:
if item not in dt:
dt[item] = 1
else:
dt[item] += 1
return dt
def fun_list(str):
lt = []
for item in str:
lt.append(item)
return fun_dict(lt)
str1 = "aaaccccdef"
print(fun_list(str1))
执行结果:
{‘a’: 3, ‘c’: 4, ‘d’: 1, ‘e’: 1, ‘f’: 1}
def fun_9():
for i in range(1,10):
for j in range(1,i+1):
print("{}*{}={}".format(i,j,i*j),end="\t")
print()
fun_9()
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
def fun_str():
s=input('请输入字符串:')
letters,space,digit,others=0,0,0,0
for c in s:
if c.isalpha():
letters+=1
elif c.isspace():
space+=1
elif c.isdigit():
digit+=1
else:
others+=1
print('英文字母={},空格={},数字={},其他字符={}'.format(letters,space,digit,others))
fun_str()
请输入字符串:werwe34534 e564
英文字母=6,空格=2,数字=8,其他字符=0
#求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字
def fun_a():
'''
2+22+222+2222+22222
可以理解为:
20000 + 2*2000 + 3*200 + 4*20 + 5*2
也就是:
1*2*10^4 + 2*2*10^3 + 3*2*10^2 + 4*2*10^1 + 5*2*10^0
所以简单迭代就可以出结果
'''
a=2
t=5
num=0
for i in range(1,t+1):
num+=i*a*(10**(t-i))
print(num)
fun_a()
def fun_tm():
s=0
t=1
for n in range(1,21):
t=t*n
s=s+t
print(s)
fun_tm()
def fun_output(s,l):
if l==0:
return
print(s[l-1])
fun_output(s,l-1)
def fun_tm1():
s=input('请输入字符串:')
l=len(s)
fun_output(s,l)
fun_tm1()
def tm_2():
num=12321
s=str(num)
for i in range(len(s)//2):
if s[i]!=s[-i-1]:
print(False)
break
else:
print(True)
tm_2()
def tm036():
'''
素数就是质数
'''
arr = [2]
for i in range(3,100):
for j in arr:
if i%j==0:
break
else:
arr.append(i)
print(arr)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
def tm_3():
a=[1,4,6,8,9,23,24,55,88]
b=99
for i in a:
if b<i:
a.insert(a.index(i),b)
break
else:
a.append(b)
print(a)
tm_3()
写的时候注意一下python切片是[-7:-4)左闭右开的,不包含[-4]的,所以要写成-3才能取到
def num():
a=123456
b=str(a)
print(b[-3:-1])
num()
def fun_tm(list):
max_a=max(list)
i=list.index(max_a)
list[0],list[i]=list[i],list[0]
print(list)
min_b=min(list)
j=list.index(min_b)
list[-1],list[j]=list[j],list[-1]
print(list)
lt=[7,2,3,6,8,5,4]
fun_tm(lt)
def fun_tm():
while True:
c=input("请输入字符:")
if c=="#":
break
else:
with open('1.log', 'a') as f:
f.write(c)
fun_tm()
def fun_tm1():
with open('1.log',r) as f:
a=f.read()
with open('2.log',r) as f:
b=f.read()
with open('3.log',a) as f:
f.write(a+b)
fun_tm1()
person = {"li":18,"wang":50,"zhang":20,"sun":22}
def fun_tm4():
dt=sorted(person.items(),key=lambda x:x[1],reverse=True)
person1={}
for i in dt:
person1[i[0]]=i[1]
print(person1)
fun_tm4()
l1=[1,2,3]
l2=[2,3,1]
l3=[1,4,3]
def fun_t1(l1,l2):
lt=[]
for i in l1:
if i in l2:
lt.append(i)
if len(lt)==len(l2):
print(True)
else:
print(False)
fun_t1(l1,l3)
def fun_s(s):
li=[]
li1=[]
for i in s:
if s.count(i)>1:
li.append(i)
else:
li1.append(i)
print(li)
print(li1)
s='wertyt'
fun_s(s)
import os
def test():
path=os.path.dirname(os.path.dirname(__file__))
filename=os.path.basename(path)
docname=os.path.dirname(path)
return filename,docname
print(test())
class One_Bject:
obj=None
def __new__(cls, *args, **kwargs):
if obj is not None:
cls.obj=super().__new__(cls)
return cls.obj
if __name__ == '__main__':
obj=One_Bject
obj1=One_Bject
print(id(obj))
print(id(obj1))
执行结果:
2371821596192
2371821596192
def fun(list):
dic={}
for i in list:
if i not in dic:
dic[i]=1
else:
dic[i]+=1
print(dic)
fun([12,3,4,5,3,2])
def fun1(list):
dic={}
for index,item in enumerate(list):
dic[item]=dic.get(item,0)+1
print(dic)
fun1([12,3,4,5,3,2])
g = os.walk('D:\second_hand_car\SecondHand_list_query')
for path, dir_list, file_list in g:
for dir_name in dir_list:
print(os.path.join(path,dir_name))
for file_name in file_list:
print(os.path.join(path,file_name))
例如输入:[1,2,3,3,3]输出3
例如输入:[1,2,3]输出False
def tm_2(s):
for i in s:
if s.count(i)>len(s)*0.5:
print(i)
break
else:
print(False)
s=[2,1,2,1]
tm_2(s)