簡體   English   中英

在不使用任何預定義的Python函數的情況下查找列表中的運行

[英]Finding a Run in a List Without using any pre-defined Python Functions

我試圖在不使用Python中的預定義列表函數的情況下在列表中查找運行,例如枚舉,zip,索引等。

通過運行,我的意思是給出一個列表[1,2,3,6,4],我想返回一個新列表[1,2,3]以及返回運行開始和結束的位置,以及運行。

下面是我對此的嘗試,但我似乎無法得到它,我似乎總是遇到問題,我得到索引超出范圍錯誤。

def run_cards (hand_shuffle, hand_sample):

    true_length = len(hand_shuffle) - 1
    y = 0
    r = []
    for x in range(len(hand_shuffle)):
        y = x + 1

        if y <= true_length: #Checks if y is in range with x 
            t = hand_shuffle[y] - hand_shuffle[x] #Subtracts the two numbers to see if they run
            r.append(t)

    lent = len(hand_shuffle)

    if hand_shuffle[lent-1] - hand_shuffle[lent-2] == 1:
        r.append(1)
    h = []

    for i in range(len(r)):

        if r[i] == 1: #If t from above for loop is equal to 1 append it to the new list
            h.append(i)
            h.append(i+1)

    p = []

    for j in h:
        p.append(hand_shuffle[j])

    print (p) 
    return hand_shuffle, hand_sample

現在,我的代碼運行,但沒有給我我想要的東西。

zipenumerate以前的答案的較少版本:

def solve(lis):
    run_length = 0
    ind = 0
    for i in range(len(lis)-1):
        x, y = lis[i], lis[i+1]
        if run_length and y-x != 1:
            break
        if y-x == 1:
            if not run_length:
                ind = i
            run_length += 1
    if run_length:
        return run_length+1, ind
    return -1

演示:

>>> solve([1,2,3,6,4])
(3, 0)                  #run length, index
>>> solve([0, 0, 0, 4, 5, 6])
(3, 3)
>>> solve([4, 5, 5, 1, 8, 3, 1, 6, 2, 7])
(2, 0)
>>> solve([1, 1, 1, 2, 3, 5, 1, 1])
(3, 2)
>>> solve([1, 1, 1, 1])
-1
>>> solve([1, 2, 5, 6, 7] )
(2, 0)
>>> solve([1, 0, -1, -2, -1, 0, 0])
(3, 3)
>>> runFind(L)
[(4, 0), (2, 3), (2, 4)]
>>> def runFind(L):
...   i = 1
...   start = 0
...   answer = []
...   while i < len(L):
...     if L[i] != L[i-1]+1:
...       answer.append((i-start, start))
...       start = i
...     i += 1
...   answer.append((i-start, start))
...   
...   return answer
... 
>>> L = [1,2,3,6,4]
>>> runFind(L)
[(3, 0), (1, 3), (1, 4)]
>>> L = [0, 0, 0, 4, 5, 6]
>>> runFind(L)
[(1, 0), (1, 1), (1, 2), (3, 3)]
>>> L = [4, 5, 5, 1, 8, 3, 1, 6, 2, 7]
>>> runFind(L)
[(2, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)]
>>> L = [1, 1, 1, 2, 3, 5, 1, 1]
>>> runFind(L)
[(1, 0), (1, 1), (3, 2), (1, 5), (1, 6), (1, 7)]
>>> L = [1, 1, 1, 1]
>>> runFind(L)
[(1, 0), (1, 1), (1, 2), (1, 3)]
>>> L = [1, 2, 5, 6, 7]
>>> runFind(L)
[(2, 0), (3, 2)]
>>> L = [1, 0, -1, -2, -1, 0, 0]
>>> runFind(L)
[(1, 0), (1, 1), (1, 2), (3, 3), (1, 6)]

只有兩個標志,其中一個標志代表開始,一個代表結束。

runs = []
indices = []
for start in range(len(hand)):
    run = [hand[start]]
    for end in range(start+1,len(hand)):
        if hand[end] == hand[end-1]:
            run.append(hand[end])
        else:
            if len(run) > min_run_length:
                runs.append(run)
                indices.append((start,end))
            break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM