2017-08-10 36 views
1

我是新來的編碼,所以我提前道歉,如果我問的是簡單的或沒有多大意義,但我會盡量詳盡闡述。首先,這不是針對任何工作或項目,我只是爲了學習一些編碼而學習我的滿意。我一直在努力尋找一些真正的生活問題來應用於編碼(主要是僞代碼,但python語言對我來說也是可以理解的)。如何比較列表中的4個連續元素?

我希望能夠有一個x元素的列表,並順序比較4個元素。

例如,myList = [a, b, c, d, e, f, g, h, i, j, k, l]

首先,我想比較A,B,c和d。 如果b>a, c>b, d>c和d> 3以前的所有(d>a, d>b, d>c)我想做點什麼,否則去下一個比較。

然後我想比較b,c,d和e。 同樣,如果c>b, d>c, e>d和e> 3以前的所有(e>b, e>c, e>d)我想做點什麼,否則去下一個比較。

如果我的列表包含無限元素,該怎麼辦? myList = [:] 我從哪裏開始?我必須有一個起點嗎?

我猜我必須使用for循環遍歷列表,但我老實說不知道如何遍歷前4個元素,然後繼續從4個元素批次的第二個元素。

由於我目前正在學習數組和列表,可能是我缺少一些功能?或者我只是我的大腦可以掌握它。

我試着看着其他帖子在stackoverflow,但老實說,我不能從其他人的答案搞清楚。我將不勝感激任何幫助或指導。

在此先感謝。

+0

d 以前的所有3個(d> a,d> b,d> c)..這是否滿足(或)我是否缺少某些東西? – Transhuman

+0

哎呀對不起,我一定錯了(我編輯了我原來的帖子)。我打算寫b> a,c> b,d> c和d> a,d> c ... – solidsn2004

回答

1

您可以使用內置的all()功能對於這個問題:

myList = [5, 4, 3, 6, 3, 5, 6, 2, 3, 10, 11, 3] 

def do_somthing(): 
    #your code here 
    pass 

for i in range(len(myList)-4): 
    new_list = myList[i:i+4] #here, using list slicing to jump ahead four elements. 
    if all(new_list[-1] > b for b in new_list[:-1]) and all(new_list[:-1][c] > new_list[:-1][c+1] for c in range(len(new_list)-2)): 
     do_something() 
1
L = [...] 
# get all the valid indices of the elements in the list, except for the last 4. These are the indices at which the 4-element windows start 
for i in range(len(L)-4): 
    window = L[i:i+4] # the 4 elements you want to compare 
    print("I am considering the elements starting at index", i, ". They are:", window) 
    a,b,c,d = window 
    if d>a>b>c<d and d>b: 
     print("The checks pass!") 

現在,有做這個簡單的方法:

for a,b,c,d in (L[i:i+4] for i in range(len(L)-4): 
    if d>a>b>c<d and d>b: 
     print("The checks pass!") 
1

既然你可以索引列表,如何從索引0開始,比較第0,第(0 + 1),第(0 + 2)和第(0 + 3)個元素。然後,在下一輪中,將索引增加到1,並比較第1個,第(1 + 1)個,第(1 + 2)個和第(1 + 3)個元素,依此類推。對於第n輪,您比較n,n + 1,n + 2和第(n + 3)個元素,直到您到達結束之前的第4個元素。這就是你通常做的事情,比如'每次從一個長度爲n的序列中測試m個元素',並且你可以很容易地將這個模式擴展爲矩陣或者3d數組。您在其他答案中看到的代碼基本上都是這樣做的,Python中的某些功能使這項工作非常簡單。

現在,'如果列表包含無限元素'呢?那麼,你需要一個生成器,在我認爲的這個階段有點高級,但這個概念很簡單:你讓一個函數讀取(可能是無限的)循環中的無限元素流,設置一個遊標對他們中的一個,回報(yield)光標下的元素,以及每次它後面的3個元素,而下一個循環之前增加光標通過一個開始:

def unsc_infinity(somelist): 
    cur = 0 
    while True: 
     yield somelist[c:c+4] 
     cur = cur + 1 

infinity_reader = unsc_infinity(endless_stream) 
next(infinity_reader) 
# gives the 0, 1, 2, 3 th elements in endless_stream 
next(infinity_reader) 
# gives the 1, 2, 3, 4 th elements in endless_stream 
next(infinity_reader) 
# ... 

而且可以在這個循環發電機太:

for a, b, c, d in unsc_infinity(endless_stream): 
    if d>a>b>c<d and d>b: 
     do_something() 

希望有點幫助,爲您打造有關如何這樣的問題做了心理模型。

1

在同一時刻從一個迭代僅消耗一個項目,並在4操作滯後要素嘗試了一圈緩衝:

# make a generator as example of 'infinte list' 
import string 
agen = (e for e in string.ascii_lowercase) 

# initialize len 4 circle buffer 
cb = [next(agen) for _ in range(4)] # assumes there are at least 4 items 
ptr = 0 # initialize circle buffer pointer 

while True: 
    a,b,c,d = (cb[(i+ptr)%4] for i in range(4)) # get current 4 saved items 

    # some fuction here 
    print(a,b,c,d) 

    # get next item from generator, catch StopIteration on empty 
    try: 
     cb[ptr] = next(agen) 
    except StopIteration: 
     break 
    ptr = (ptr + 1)%4 # update circle buffer pointer 
a b c d 
b c d e 
c d e f 
d e f g 
e f g h 
f g h i 
g h i j 
h i j k 
i j k l 
j k l m 
k l m n 
l m n o 
m n o p 
n o p q 
o p q r 
p q r s 
q r s t 
r s t u 
s t u v 
t u v w 
u v w x 
v w x y 
w x y z 

「的一些功能」可以包括太多的停止條件:

# random.choice() as example of 'infinte iterator' 
import string 
import random 
random.choice(string.ascii_lowercase) 

# initialize len 4 circle buffer 
cb = [random.choice(string.ascii_lowercase) for _ in range(4)] # assumes there are at least 4 items 
ptr = 0 # initialize circile buffer pointer 


while True: 
    a,b,c,d = (cb[(i+ptr)%4] for i in range(4)) # get current 4 saved items 

    # some fuction here 
    print(a,b,c,d) 
    if a<b<c<d: # stopping condition 
     print("found ordered string: ", a,b,c,d) 
     break 

    # get next item from generator, catch StopIteration on empty 
    try: 
     cb[ptr] = random.choice(string.ascii_lowercase) 
    except StopIteration: 
     break 
    ptr = (ptr + 1)%4 # update circle buffer pointer 
o s w q 
s w q k 
w q k j 
q k j r 
k j r q 
j r q r 
r q r u 
q r u v 
found ordered string: q r u v