输入到列表并找到相同输入python的最长条纹

时间:2015-03-03 18:47:55

标签: python list casting int items

我正在编写一个程序,其中用户将值输入到列表中,直到想要结束它,程序将告诉用户他们输入的最长数字。例如,用户输入的是7,7,7,6,6,4,结束时会得到输出:你的最长条纹为3.连续3次输入7次。

到目前为止我有这个并且它似乎不想结束当前的运行所以如果我输入7,7,7,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)

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:1)

假设您有[7, 7, 7, 6, 6, 6, 6, 5, 4, 6, 7]之类的列表,您可以使用groupby()功能计算重复次数,然后在最后打印最大数字。

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次

答案 1 :(得分:1)

这是你所描述的如何完成的抽象版本。中途我意识到我用Python 16运行它,所以它向后兼容!

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

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

答案 2 :(得分:1)

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

我使用异常处理来输入。如果输入非数字,它将忽略它并再次询问一个数字。如果你什么都不输,它将结束输入循环。

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

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)

答案 3 :(得分:1)

>>> 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

答案 4 :(得分:0)

试试这个

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)