可以用local(),这是一个字典,包括了所有定义了的变量。
if i in locals()['list' + str(x)]:
学习了,谢谢{:10_297:} str= 'ABCDEFGHLMNOPQRMST'
str1=['A','M','T']
str2=['B','C','D','E']
str3=['N','S']
count=0
count1=0
count2=0
count3=0
for i in str:
if i in str1:
count1=count1+1
elif i in str2:
count2=count2+1
elif i in str3:
count3=count3+1
count=
print(count) 我是来领鱼币的 def strcount(str1):
group = ['AMTUVWY', 'BCDEK', 'NSZ', 'HIOX']
count= *5
for i in str1:
for j in range(len(group)):
if i in group:
count += 1
break
else:
count += 1
print("字符串里面有左右对称字符%d个,上下对称字符%d个,旋转对称有%d个,\
全对称字符%d个,其他字符有%d个"%tuple(count))
def main():
str = 'WEFNKDSPHOFGMAFW'
strcount(str)
main()
下面是IDLE的运行结果:
字符串里面有左右对称字符4个,上下对称字符3个,旋转对称有2个, 全对称子字符2个,其他字符有33个
大神为什么会这样。。。{:5_92:} 没看懂题目{:5_96:} def strcount(string):
#ABCDEFGHIJKLMNOPQRSTUVWXYZ
str_dict0 = ["AMUVWY"]
str_dict1 = ["BCDEK"]
str_dict2 = ["NSZ"]
str_dict3 = ["HIOX"]
num0 = 0
num1 = 0
num2 = 0
num3 = 0
num4 = 0
for i in string:
if i in str_dict0:
num0 += 1
#print(i)
elif i in str_dict1:
num1 += 1
elif i in str_dict2:
num2+= 1
elif i in str_dict3:
num3 += 1
else:
num4 += 1
return "字符串中共有左右对称字符{}个,上下对称字符{}个,旋转对称字符{}个,全对称字符{}个,其他字符{}个。".format(num0,num1, num2, num3, num4)
return None
str1 = 'WEFNKDSPHOFGMAFW'
strcount(str1) 本帖最后由 咕咕鸡鸽鸽 于 2019-3-5 20:15 编辑
def fun91(str1):
# 分别是 左右 , 上下 ,旋转
sym = ["AHIMOTUVWXY","BCDEHIKOX","HINOSX","HIOX"]
hor = 0
ver = 0
rot = 0
full = 0
for each in str1:
if each in sym:
full += 1
else:
if each in sym:
hor += 1
if each in sym:
ver += 1
if each in sym:
rot += 1
print("字符串中共有左右对称字符{}个,上下对称字符{}个,旋转对称字符{}个,全对称字符{}个,其他字符{}个。"\
.format(hor,ver,rot,full,len(str1) - hor - ver - rot - full))
str1 = 'WEFNKDSPHOFGMAFW'
fun91(str1)
def fun91(x):
LR = 'AMTUVWY'
UD = 'BCDEK'
ROTATE = 'NSZ'
LRUD = 'HIOX'
count = {"LR":0,'UD':0,'ROTATE':0,'LRUD':0,'ELSE':0}
for i in x:
if i in LR:
count['LR'] += 1
if i in UD:
count['UD'] += 1
if i in ROTATE:
count['ROTATE'] += 1
if i in LRUD:
count['LRUD'] += 1
else:
count['ELSE'] += 1
return '字符串中共有左右对称字符%s个,上下对称字符%s个,旋转对称字符%s个,全对称字符%s个,其他字符%s个。'%(count['LR'],count['UD'],count['ROTATE'],count['LRUD'],count['ELSE'])
if __name__ == '__main__':
print(fun91('WEFNKDSPHOFGMAFW'))
字符串中共有左右对称字符4个,上下对称字符3个,旋转对称字符2个,全对称字符2个,其他字符1个。
>>> 康康答案 1 看看
111
学习 看看
页:
1
[2]