簡體   English   中英

從元組列表生成具有相同元組數和索引的列表范圍

[英]Generate range of lists with the same number of tuples and index from list of tuples

注意: string雖然值相同,但在處理時將返回不同的max_count值,為簡單起見, compute_string_and_return_integer(string)將生成3到9之間的隨機數。

鑒於:

#initial input
[(string, 0),(string, 0),(string,0),(string, 1)]

預期:(生成的最大范圍取決於先前的輸入)

input = [(string, 0),(string, 0),(string,0),(string, 1)]
max_count = how_many(input) #returns (3,1) #3 is total, and 1 is the 2nd item in list to modify
generate_additional_lists(input, *max_count)
#each of generated lists will aso be used as input to generate the next batch.
[(string, 0),(string, 1),(string,0),(string, 1)] #used as input in ext run
[(string, 0),(string, 2),(string,0),(string, 1)] #used as input in next run
[(string, 0),(string, 3),(string,0),(string, 1)] #used as input again

input2 = [(string, 0),(string, 1),(string,0),(string, 1)]
max_count = how_many(input2) #returns (3,2), where 2 is index which points to 3rd tuple item in the list.
generate_additional_lists(input2, *max_count)
[(string, 0),(string, 1),(string,1),(string, 1)]
[(string, 0),(string, 1),(string,2),(string, 1)]
[(string, 0),(string, 1),(string,3),(string, 1)]

input3 = [(string, 0),(string, 2),(string,0),(string, 1)]
max_count = how_many(input3) #returns (7,2) where 7 is total lists to generate, 2 is index which points to 3rd tuple item in the list.
generate_additional_lists(input3, *max_count)
[(string, 0),(string, 2),(string,1),(string, 1)]
[(string, 0),(string, 2),(string,2),(string, 1)]
[(string, 0),(string, 2),(string,3),(string, 1)]
[(string, 0),(string, 2),(string,4),(string, 1)]
[(string, 0),(string, 2),(string,5),(string, 1)]
[(string, 0),(string, 2),(string,6),(string, 1)]
[(string, 0),(string, 2),(string,7),(string, 1)]

input4 = [(string, 0),(string, 3),(string,0),(string, 1)]
max_count = how_many(input4) #returns (4,2) where 4 is the total and 2 2 is index which points to 3rd tuple item in the list.
generate_additional_lists(input4, *max_count)
[(string, 0),(string, 3),(string,1),(string, 1)]
[(string, 0),(string, 3),(string,2),(string, 1)]
[(string, 0),(string, 3),(string,3),(string, 1)]
[(string, 0),(string, 3),(string,4),(string, 1)]

#we no longer have any lists with tuples that is not first or last containing 0. We stop as we have listed every possible combination.

列表中的第一個和最后一個元組從不更改,並且始終保持不變。 在給定列表中,第一個和最后一個之間的每個元組都是焦點。 如上所示,生成的列表數取決於字符串值。

我本來以為使用itertools的笛卡爾乘積就足夠了,但這需要提前知道每個級別的元組列表。 當輸入列表確定生成多少其他列表(包含具有遞增索引的元組)時,難度會增加。

def how_many(input_list):
  for tuple_index, input in enumerate(input_list):

    if input[1] is 0: #signal to generate additional lists but how many?
       count = get_max_list_count(input[0]) #pass the string value of thhis
       return [count, tuple_index] #returns a list of how many to generate and which tuple to modify

def get_max_list_count(string_from_that_tuple):
return compute_string_and_return_integer(string_from_that_tuple) 
#for simplicitys sake, it will return a random integer between 3 and 9. The string value is not important.

def generate_additional_lists(input_list, *max_count):
#max_count[0] contains how many lists to generate
#max_count[1] contains which tuple to modify and increment it's integer value when generating the list
#generate max_count[0] number of lists with the max_count[1]th tuple containing incrementing integer

我在開始更新問題之前就開始編寫此代碼,因此它是一個示例,其中沒有您的how_many()函數,但是沒有一個虛擬函數(奇怪的是,我也叫how_many!)看看是否有幫助...

initial_input = [("This doesn't matter", 0),("Gimme 4", 0),("Maybe 8", 0),("Ignore me", 1)]

def how_many(mystring):
    return int(mystring[-1])

def display(yourlist):
    for element in yourlist:
        print element

list_of_lists = [[] for i in range(len(initial_input)-1)]
list_of_lists[0] = [list(initial_input)]

for i in range(1, len(initial_input)):
    for currlist in list_of_lists[i-1]:
        element = currlist[i]
        if element[1] == 0:
            recursions = how_many(element[0])
            additems = [currlist[:i] + [(element[0], tick)] + currlist[i+1:] for tick in range(1, recursions)]
            list_of_lists[i].extend(additems)

for i in range(len(initial_input)-1):
    print "\n"
    display(list_of_lists[i])

順便說一句,我找到了一個遞歸解決方案:

def generate_additional_lists(lst, max_count, result):
    max_count  = list(max_count)
    lst = [list(i) for i in lst]
    if max_count[0]:
        lst[max_count[1]][1]  += 1
        max_count[0] -= 1
        lst = [tuple(i) for i in lst]
        result.append(lst)
        generate_additional_lists(lst, max_count, result)
    return result

演示:

>>> new = generate_additional_lists(input1, max_count, [])
>>> for i in new:
    print(i)


[('string', 0), ('string', 1), ('string', 0), ('string', 1)]
[('string', 0), ('string', 2), ('string', 0), ('string', 1)]
[('string', 0), ('string', 3), ('string', 0), ('string', 1)]

純娛樂!

暫無
暫無

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

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