2020银⾏Python基础⾯试笔试⼿撕代码题,整理了题型和⾃⼰测试的结果
2020⾯试笔试,参加的有浦发笔试⾯试,农⾏研发,中⾏,中信,京东⽹易数据挖掘,以及⼀些商业银⾏题型,⾄于腾讯百度的太难了,本⼈直接放弃,⾮计算机专业,纯⾃学python。
整理了⾃⼰练习的Python基础:
偶数求和
# 偶数求和
sum([i for i in range(101)if i%2==0])
sum=0
for i in range(0,101,2):
sum+= i
print(sum)
something = adline().strip()
请输出奇数位字母
# 请输出奇数位字母
st ='Hello Python DuShuSir'
sr =[]
i=0
while i<len(st):
if i%2!=0:
sr.append(st[i])
i +=1
print(sr)
请输出纯数字项
>>>str="h3110 23 cat 444.4 rabbit 11 2 dog"
>>>[int(s)for s in str.split()if s.isdigit()]
[23,11,2]
输出数字
import re
totalCount ='100abc'
totalCount = re.sub("\D","", totalCount)
print(totalCount)
>>>100
已知⼀个⼆叉树前序遍历和中序遍历分别为ABDEGCFH和DBGEACHF,则该⼆叉树的后序遍历为?
答:DGEBHFCA
计算题,判断变量是否为0,是0则为False,⾮0判断为True
# and中含0,返回0;均为⾮0时,返回后⼀个值,bootstrap table 序号
2and0# 返回0
2and1# 返回1
1and2# 返回2
# or中,⾄少有⼀个⾮0时,返回第⼀个⾮0,
2or0# 返回2
2or1# 返回2
0or1# 返回1
#实际题⽬,⽹易的选择题好像是
a ='hello'
b =[1,2]
print(a and b)#[1, 2]
print(b and a)#hello
改变字母⼤⼩写
# 改变字母⼤⼩写
def change(text):
list_a =[]
for i in text.split(' '):
list_a.append(i.capitalize())
a =' '.join(list_a)
return a
text ='hello your world!'
change(text)
list_b =[]
for i in text.split(' '):
list_b.append(i.capitalize())
b =' '.join(list_b)
print(b)
字母⼤⼩写
s='What is Your Name?'
s2=s.lower()
print(s2)#返回⼩写字符串
# what iss your name?
print(s.upper())#返回⼤写字符串
# WHAT IS YOUR NAME?
print(s.capitalize())#字符串⾸字符⼤写
# What is your name?
print(s.title())#每个单词的⾸字母⼤写
# What Is Your Name?
print(s.swapcase())#⼤⼩写互换
# wHAT IS yOUR nAME?
#提取⼀个整数的偶数位
# 第⼀题:提取⼀个整数的偶数位
# 1.输⼊123456,输出246
list[::2]#就是取奇数位这⾥的 i j 我们省略的话就是默认数组最开头到结尾
list[1::2]#这⾥缺省了j 但是i定义了1 也就是从数组第⼆个数开始取,所以这个是取偶数位
a =int(12345678)
b =list(str(a))
c =[]
for i, num in enumerate(b):
if i%2!=0:
c.append(num)
print(''.join(c))wwii
统计⼀个字符串中的字符和数字出现个数
# 统计⼀个字符串中的字符和数字出现个数
# 2.输⼊a3rcd57sc8,输出数字个数 4 字符个数 6
intCount =0#⽤来记录列表中的int元素个数
strCount =0#记录str元素个数
spacecount =0
otherCount =0
a ="pl421z inp42ut442 a string343bgb2 gfregfe%%%%%"
# 使⽤for循环遍历字符串,每次循环判断当前获取的元素的类型,并给对应计数器计数
for i in a:
if i.isdigit():#判断i是不是int
intCount +=1
elif i.isalpha():#判断i是不是str
strCount +=1
elif i.isspace():
spacecount +=1
else:
otherCount +=1
print("数字=%d,字母=%d,其他=%d, 空格%d"%(intCount,strCount,otherCount,spacecount)) A-Z对应1-26,输⼊⼀串字符,输出对应的数。
# A-Z对应1-26,输⼊⼀串字符,输出对应的数。
l1 =['a','b','c','d','e']
l2 =[1,2,3,4,5]
a =input()mybatis 代码生成器
dict_1 =dict(zip(l1,l2))
print(dict_1[a])
⼦字符串
# ⼦字符串
def cut(s:str):
results =[]
# x + 1 表⽰⼦字符串长度
for x in range(len(s)):
# i 表⽰偏移量
for i in range(len(s)- x):
results.append(s[i:i + x +1])
return results
print(cut('abc'))
统计字符个数
#统计字符个数
str=input("请输⼊⼀串字符:")
resoult={}
while是什么词性for i in set(str):
resoult[i]=unt(i)
print(resoult)
def count_char(test_str):
"""统计字符串的个数"""
count_dict ={}# 存储统计结果的字典
for k in set(test_str):
if k ==" ":
continue
count_dict[k]= unt(k)
return count_dict
test_str ="hello world"
count_dict = count_char(test_str)
print(count_dict)
判断闰年
def mian():
year =int(input('请输⼊年份: '))
# 如果代码太长写成⼀⾏不便于阅读可以使⽤\或()折⾏
is_leap =((year %4==0and year %100!=0)or year %400==0) print(is_leap)
if __name__ =='__main__':
main()
词频统计,统计计数
#词频统计
a =[1,2,3,2,1,5,6,5,5,5]
b =set(a)
for each_b in b:
count =0
for each_a in a:
if each_b == each_a:
count +=1
print(each_b,": ", count)
# 统计计数
a =[1,2,3,2,1,5,6,5,5,5]
b ={}
for i in a :
b[i]= a.count(i)
把原先的nodejs卸载删除干净
print(b)
# 字典、列表计数
list=[1,2,1,2,3,3,4,5,4]
set=set(list)
dict={}
for item in set:
dict.update({unt(item)})
print(dict)
l ='give me an offer'
a =list(l.split(' '))京东python入门教程
d ={}
for i in a:
d[i]= l.count(i)
print(d)
# 或者
l ='give me an offer'
a =list(l.split(' '))
d ={}
for i in a:
d[i]=len(i)
sort_str =max(d.items(),key =lambda x: x[1])
print(sort_str)
#利⽤pandas模块下的value_counts⽅法(这⾥需要安装pandas模块)import pandas as pd
list=[1,2,1,2,3,3,4,5,4]
result=pd.value_counts(list)
print(result)
# 按照sort的value排序,默认降序
import pandas as pd
list=[1,2,1,4,4,1,1,1,5,2,2,2,2,3,3,3,4,4,4,4,3,4,5,4]
result=pd.value_counts(list,sort =True,ascending =True)
print(result)
n =int(input())
b =int(input())
count =0
for i in range(1,n+1):
if i%b ==0:
count +=1
print(count)
字符串反转

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。