Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输入列表并查找同一输入的最长条纹_Python_List_Casting_Int_Items - Fatal编程技术网

Python 输入列表并查找同一输入的最长条纹

Python 输入列表并查找同一输入的最长条纹,python,list,casting,int,items,Python,List,Casting,Int,Items,我正在写一个程序,在这个程序中,用户将值输入到一个列表中,直到用户想要结束它,程序将告诉用户他们输入的最长的连续数字。例如,如果用户输入了7,7,7,6,6,4,那么最终将得到输出:您的最长条纹是3。As 7连续输入3次 到目前为止,我有这个,它似乎不想结束当前运行,所以如果我输入7,7,7,6,6,6,6,6,5,4,它会告诉我最长的连胜是7,就像它是从输入的7继续连胜一样。这就是我所拥有的: mylist = [] run = 1 currentrun = 1 number = inp

我正在写一个程序,在这个程序中,用户将值输入到一个列表中,直到用户想要结束它,程序将告诉用户他们输入的最长的连续数字。例如,如果用户输入了7,7,7,6,6,4,那么最终将得到输出:您的最长条纹是3。As 7连续输入3次

到目前为止,我有这个,它似乎不想结束当前运行,所以如果我输入7,7,7,6,6,6,6,6,5,4,它会告诉我最长的连胜是7,就像它是从输入的7继续连胜一样。这就是我所拥有的:

mylist = []

run = 1

currentrun = 1

number = input('enter a number: ')
mylist.append(number)



while number != 'end' :
    number = input ('enter a number: ')
    mylist.append(number)
for i in range (len(mylist)):

    if mylist[i] == mylist[i-1] and mylist[i] == mylist[i+1] :
        currentrun = currentrun + 1
    else:
        currentrun = 0

    print (currentrun)
    if currentrun > run:
        run = currentrun

print (mylist)

print ('Your longest run was' ,run)
非常感谢您的帮助。

试试这个

mylist = []
while True:
    mylist.append(int(raw_input("enter number:")))
streak = 0
cur, last = 0, None
for num in mylist:
    if num == last:
        curr += 1
    else:
        streak = max(streak, cur)
        last = num
        cur = 0
print("longest run was ",streak)

假设您有一个列表,如
[7,7,7,6,6,6,5,4,6,7]
,您可以使用该函数计算重复次数,然后在末尾打印最大次数

from itertools import groupby
a = [7, 7, 7, 6, 6, 6, 6, 5, 4, 6, 7]
lst = []
for n,c in groupby(a):
   num,count = n,sum(1 for i in c)
   lst.append((num,count))

maxx = max([y for x,y in lst])
print 'Your longest run was {}'.format(maxx)

在本例中,它返回4,因为数字6连续重复了4次

这是您描述的操作方式的一个详细版本。中途我意识到我是用Python16运行它的,所以它是向后兼容的

a = None # stores the last number seen
b = 0 # stores the count of the last number
result = [0, 0] # number, count result array
for c in "7,7,7,6,6,6,6,5,4".split(','): # split string into array of
                                         # just our numbers
    c = int(c) # change the character into a bast ten int
    if c != a: # check current number against last
        a = c # if different than last, remember current number as last
        b = 1 # start over counting at one
    else: # if current number is same as last
        b = b + 1 # increment counter
        if b > result[1]: result = a, b # if counter higher than highest
                                        # previous count, store the
                                        # current number and count
print(("value: %i, count: %i" % result)) # print resulting number, count
输出:

value: 6, count 4

如果您有任何问题,请随时发表评论。

这将跟踪streak的值。如果另一条条纹更长,它将用新的值和长度替换最后一条条纹

我对输入使用了异常处理。如果您输入一个非数字,它将忽略该数字并再次要求输入一个数字。如果不输入任何内容,将结束输入循环

请注意,我使用的是而不是输入

while True:
    number = raw_input('enter a number: ')
    if number == '':
      break

    try:
        number = int(number)
    except ValueError:
        print ("'" + number + "' is not a number.")
        continue

    mylist.append(number)

if len(mylist) > 0:
    #print (mylist)

    # Chain is the current run we're tracking. Longest is the longest chain we've tracked so far.
    chain = longest = 1
    # Current is the value of the chain we're tracking now. Value is the value of the longest chain we've tracked so far. We use the first value of the list.
    current = value = mylist[0]

    # Starting with the second number in the list and iterating to the end we compare values.
    for number in mylist[1:]:
        # Did we find another in our current chain?
        if number == current:
            chain += 1
        else:
            chain = 1
            current = number

        # This will require chains to exceed the previous longest chain to be chosen as the longest. Change this to >= to track the last chain (in the case of a tie).
        if chain > longest:
            longest = chain
            value = current

    print ('Your longest run was', longest)

你正在吃
ValueError
;一个坏习惯。@MotokoKusanagi你在一个小的技术问题上责备我。是的,我们应该通知用户他们输入的值无效。但是,我们也应该告诉用户如何退出循环。这是OP没有做的事情,我觉得答案中没有必要做。我让OP来填补UX的空白。添加所有必需的错误检查和用户反馈不是问题的一部分。
>>> from itertools import groupby
>>> input_iter = iter(lambda: input('enter a number: '), 'end')
>>> max(sum(1 for x in v) for k,v in groupby(input_iter))
enter a number: 7
enter a number: 7
enter a number: 7
enter a number: 6
enter a number: 6
enter a number: 4
enter a number: end
3