【问题标题】:Generate range of lists with the same number of tuples and index from list of tuples从元组列表生成具有相同数量的元组和索引的列表范围
【发布时间】:2013-11-14 01:06:49
【问题描述】:

注意: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

【问题讨论】:

  • 为什么前两个有三个列表,后两个分别有 7 和 4?
  • 你有确定生成列表数量的函数吗?还是这 4 个字符串值只有 4 个固定数字?
  • 另外,它应该如何决定更改第一个列表中的 [1][1]、第二个列表中的 [2][1]、第三个列表中的 [2,1] 和 [ 2][1] 在最后?
  • @ILovePython 你能也显示字符串值吗?
  • @ILovePython 这似乎是一条重要的信息,没有它我们无法解决问题。我没有看到可以在这里应用的底层抽象模式。

标签: python


【解决方案1】:

我在你更新问题之前就开始写这篇文章了,所以这是一个没有你的 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])

【讨论】:

  • 我不敢相信你明白了
  • 我希望我能投票,但我不能,因为我缺乏声望点。需要 15 个。
  • 很高兴为@ILovePython 提供帮助! :)
【解决方案2】:

顺便说一下,我找到了递归解决方案:

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

只是为了好玩!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多