我有一个包含服务器名的列表,比如['oracle0123','oracle0124','oracle0125']
。我想检查服务器名称的末尾有多少个数字,因为这是不同的(在本例中,它是4)。我对如何做到这一点有一个模糊的想法,但我的方法不起作用。你知道吗
v=['oracle0123','oracle0124','oracle0125']
def get_num_position(v):
for i in v:
i=i[::-1]
print('reverse server is-',i)
for j in i:
x=0
if j.isdigit():
x = x+1
print(x)
return x
get_num_position(v)
这对你很有用。你知道吗
您还可以使用^{} 来实现这一点:
请注意,如果输入字符串未以数字结尾,则上述代码将失败:
在Python3中,可以使用
*
赋值来避免此类错误:问题是您正在为每个字符将
x
的值重置为0。而且,我猜您只想在循环遍历每个单词之后才打印x
。这应该在不太改变代码逻辑的情况下工作:相关问题 更多 >
编程相关推荐