gpt4 book ai didi

Python:如何检测长序列号中的特定数字

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:50 25 4
gpt4 key购买 nike

我一直在做这个项目,一个小的 Python 和 Tkinter 项目,因为我是一个初学者,如果不是因为我在做一些测试后发现的这个小问题,我几乎完成了它。该程序应该说我在输入中输入的序列号是否是“魔鬼号码”,这取决于该号码是否包含数字“666”。在积极的情况下,它应该有数字“666”并且它应该远离其他 6,这意味着不应该有像这个“666”这样的东西。如果数字“666”在序列号中重复多次(没有粘在一起的“666666”),也可以认为是魔鬼号码。

我遇到的问题是,当我测试其中只有一个“666”并且同时以该数字 (666) 结尾的数字时,这些数字不被视为魔鬼数字,而它们应该是。我似乎无法解决这个问题。

为了实现这个项目,我使用了 Python 和 Tkinter。代码如下:

"""*************************************************************************"""
""" Author: CHIHAB Version: 2.0 Email: chihab2007@gmail.com """
""" Purpose: Practice Level: Beginner 2016/2017 """
"""*************************************************************************"""
############################ I M P O R T S#####################################
from tkinter import*
from types import *
############################ T K I N T E R ####################################
main = Tk()

e = Entry(main, bg="darkblue", fg="white")
e.pack(fill=X)

l = Label(main, bg="blue", fg="yellow")
l.pack(fill=X)
############################ F U N C T I O N S ################################
def devil(x): #TEST ENTERED VALUE FOR DEVIL NUMBER
c = 0
i = 0
l = list(x)
while i < len(l): #This block of code means that as long as the index i
if l[i] == "6": # is below the length of the list to which we have
c = c+1 # converted the entry, the program is allowed to keep
print("this is c :", c) # reading through the list's characters.
else:
c = 0
if i <= (len(l)-2) and c == 3 and l[i+1] != "6":
return True
i = i+1
return False
def printo(): #GET VALUE ENTRY AND SHOW IT IN LABEL
x = e.get()
if x != "":
if x.isnumeric() == True: #SHOW ENTERED VALUE IF INTEGER
y = devil(x)
if y == True:
print("The number you entered is a devil number.")
l.config(text="The number you entered is a devil number.", bg="blue")
else:
print("The number you entered is NOT a devil number.")
l.config(text="The number you entered is NOT a devil number.", bg="blue")
#print(x)
e.delete(0, END)
else: #SHOW ERROR IF NOT INTEGER
l.config(text="please enter an integer in the entry.", bg="red")
print("please enter an integer in the entry.")
e.delete(0, END)
else: #SHOW ERROR IF EMPTY
l.config(text="please enter something in the entry.", bg="red")
print("please enter something in the entry.")

############################ T K I N T E R ####################################
b = Button(main, text="Go", bg="lightblue", command=printo)
b.pack(fill=X)

main.mainloop()

给你,伙计。我希望我的代码足够简洁,并且你能够帮助我,我对此毫不怀疑。谢谢。

最佳答案

如果你的意思是666,找到任何地方的数字应该是匹配的,那么就很简单了:

if '666' in '1234666321':
print("It's a devil's number")

但是,你说666一定是一个“单独的”666,即恰好三个6并排,不多,不较少的。不是两个,也不是四个。五个 6 就出来了。在那种情况下,我会使用 tobias_k's regex .

不过,如果您非常讨厌正则表达式,您可以使用 string.partition 来实现:

def has_devils_number(num):
start, mid, end = num.partition('666')
if not mid:
return False
else:
if end == '666':
return False
elif start.endswith('6') or end.startswith('6'):
return has_devils_number(start) or has_devils_number(end)
return True

这是性能的样子:

>>> x = '''
... import re
... numbas = ['666', '6', '123666', '12366', '66123', '666123', '666666', '6666', '6'*9, '66661236666']
...
... def devil(x):
... return re.search(r"(?:^|[^6])(666)(?:[^6]|$)", x) is not None
... '''
>>> import timeit
>>> timeit.timeit('[devil(num) for num in numbas]', setup=x)
13.822128501953557

>>> x = '''
... numbas = ['666', '6', '123666', '12366', '66123', '666123', '666666', '6666', '6'*9, '6666123
666']
... def has_devils_number(num):
... start, mid, end = num.partition('666')
... if not mid:
... return False
... else:
... if end == '666':
... return False
... elif start.endswith('6') or end.startswith('6'):
... return has_devils_number(start) or has_devils_number(end)
... return True
... '''
>>> timeit.timeit('[has_devils_number(num) for num in numbas]', setup=x)
9.843224229989573

我和你一样惊讶。

关于Python:如何检测长序列号中的特定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38769350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com