2016-03-04 65 views
0

根據特定模式創建列表的簡單方法是什麼?以x開始,加1,加3,加1,加3,...創建一個遵循特定創建模式的列表

我想出了這個方法,但肯定有一個更好(更緊湊)的方式:

i = 0 
n = 100 
l = [] 
for x in range(int(n/2)): 
    i = i + 1 
    l.append(i) 
    i = i + 3 
    l.append(i) 

這創建列表

[1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29, 32, 33, 36, 37, 40, 41, 44, 45, 48, 49, 52, 53, 56, 57, 60, 61, 64, 65, 68, 69, 72, 73, 76, 77, 80, 81, 84, 85, 88, 89, 92, 93, 96, 97, 100, 101, 104, 105, 108, 109, 112, 113, 116, 117, 120, 121, 124, 125, 128, 129, 132, 133, 136, 137, 140, 141, 144, 145, 148, 149, 152, 153, 156, 157, 160, 161, 164, 165, 168, 169, 172, 173, 176, 177, 180, 181, 184, 185, 188, 189, 192, 193, 196, 197, 200] 

什麼更復雜的圖案,像+1,-2,+ 3,+ 1,-2,+ 3,...

+0

你的代碼此時會拋出一個'TypeError'。你的例子中期望的輸出是什麼? – gtlambert

+0

對不起,那裏有幾個(啞)語法錯誤。我現在修好了! – reox

回答

1

的Python在itertools提供了cycle功能對此有幫助:

from itertools import cycle 

i = 0 
n = 10 
l = [] 
step = cycle([1, -2, 3]) 

for _ in range(n): 
    i += next(step) 
    l.append(i) 

print(l) 

給你:

[1, -1, 2, 3, 1, 4, 5, 3, 6, 7] 

或者你也可以做以下一個班輪:

from itertools import accumulate, islice, cycle 
import operator 

l = list(accumulate(islice(cycle([1, -2, 3]), 10), operator.add)) 
print(l) 

accumulate在版本中添加3.2

+0

只要'i == 0','accumulate'版本就不錯... –

+0

@JonClements不,你可以在這裏使用這個,給它一個任意的起始數字:'l = list(accumulate([10] + list(islice(cycle([1,-2,3]),9)),operator.add))'idk,可能不是最優雅的方式,但它可以工作 – reox

+0

@reox哦 - 它可以以各種方式工作 - 我只是指出,所提出的積累解決方案的巧妙之處在於,'i == 0'起作用,但它與用於循環的for循環不相同,其中'i!= 0' –

0
# Function 
# Can accept an existing list to be added to (_result_list) which is optional 
# If _result_list is not included it is created from scratch 

def AddList(_factors_list, _result_list = []): 
    # Loop through the _factors_list (values to be added) 
    for _value in _factors_list: 
     # If _result_list (either passed in or created by function) contains 
     # no values, add the first item from _factors_list as the first entry 
     if len(_result_list) < 1: 
      _result_list.append(_value) 
      continue # Go to the next item in the _factors_list loop 
     # If _result_list already has values 
     else: 
      # _result_list[len(_result_list) - 1] takes the last item in list 
      # Don't use 'pop' as that removes the item 
      # Append the next item as 'last item' + 'current_loop_value' 
      _result_list.append(_result_list[len(_result_list) - 1] + _value) 
      continue # Go to the next item in the _factors_list loop 
    #Def all done. Return the _result_list 
    return _result_list 

result_list = [] # Optional, but can be an existing list 
factors_list = [1,-2,3, 5, -4] # Items to be added 
# test without existing list 
result_list = AddList(factors_list) 
print result_list 

# test WITH existing list 
result_list = [9, 8, 3] # Optional, but can be an existing list 
factors_list = [1,-2,3, 5, -4] # Items to be added 
result_list = AddList(factors_list, result_list) 
print result_list 

編輯: 你可以n也使用隨機函數

import random 
result_list = AddList(random.sample(range(-100, 100), 10)) 
print result_list