【问题标题】:How do I extend one list to the length of another?如何将一个列表扩展到另一个列表的长度?
【发布时间】:2019-08-14 02:06:01
【问题描述】:

这是我的代码:

difference = len(L4)-len(L3)
if difference == 0:
    pass
elif difference > 0:
    x = L3[0:difference]
    L3.extend(x)
elif difference < 0:
    x = L4[0:difference]
    L4.extend(x)

L4 和 L3 是两个独立的列表,我希望它们的长度相同。如果 L3 更小,我希望列表 L3 扩展到 L4 的大小,反之亦然。

示例一个输入:

0;NATE;NATHAN      #NATE is L3, NATHAN IS L4

示例一输出:

[78, 65, 84, 69, 78, 65]     #L3
[78, 65, 84, 72, 65, 78]     #L4

*这里,列表 L3 扩展到列表 L4 的长度。

示例二输入:

0;NAT;DNADNANNFNDFGDFGFGF

示例二输出:

[78, 65, 84, 78, 65, 84]
[68, 78, 65, 68, 78, 65, 78, 78, 70, 78, 68, 70, 71, 68, 70, 71, 70, 71, 70]

在多次测试我的代码后,输出代码的第一行 L3 似乎会在停止之前迭代两次,因此如果 L4 非常长,L3 将不会延伸到相同的长度。我该如何解决这个问题?

【问题讨论】:

    标签: python list append extend


    【解决方案1】:

    如果我没有过多地更改您的代码,您可以尝试以下方法:

    解决方案 1

    L3 = list('NAT')
    L4 = list('DNADNANNFNDFGDFGFGF')
    
    difference = len(L4)-len(L3)
    if difference == 0:
        pass
    elif difference > 0:
        num_times, remainder = divmod(len(L4), len(L3))
        x = L3 * num_times + L3[:remainder]
        L3 = x
    elif difference < 0:
        num_times, remainder = divmod(len(L3), len(L4))
        x = L4 * num_times + L4[:remainder]
        L4 = x
    
    L3 = list(map(ord, L3))
    L4 = list(map(ord, L4))
    
    print(L3, '\n', L4, sep='')
    

    但如果我们想简化,我们可以有这样的东西:

    解决方案 2

    L3 = list('NAT')
    L4 = list('DNADNANNFNDFGDFGFGF')
    
    size_of_bigger = max(len(L3), len(L4))
    
    num_times, remainder = divmod(size_of_bigger, len(L3))
    L3 = L3 * num_times + L3[:remainder]
    
    num_times, remainder = divmod(size_of_bigger, len(L4))
    L4 = L4 * num_times + L4[:remainder]
    
    output = "\n".join(str(list(map(ord, x))) for x in [L3, L4])
    print(output)
    

    如果我们将重构推得太远:

    解决方案 3

    L3 = list('NAT')
    L4 = list('DNADNANNFNDFGDFGFGF')
    
    input_lists = [L3, L4]
    
    size_of_bigger = max(map(len, input_lists))
    
    def output_lists(input_lists):
        for current_list in input_lists:
            num_times, remainder = divmod(size_of_bigger, len(current_list))
            yield current_list * num_times + current_list[:remainder]
    
    output = "\n".join(str(list(map(ord, x))) for x in output_lists(input_lists))
    print(output)
    

    【讨论】:

      【解决方案2】:

      您可以使用 itertools 库中的 cycle 和 islice。

      from itertools import cycle, islice
      
      def process(shorter, aim):
        #shorter is a lit of int
        #aim is the targeted len
        return list(islice(cycle(shorter), aim))
      
      L3 = [ord(i) for i in 'NATE' ]
      L4 = [ord(i) for i in 'NATHAN']
      difference = len(L4)-len(L3)
      if difference == 0:
        pass
      elif difference > 0:
        print(process(L3, len(L4)), L4)
      elif difference < 0:
        print(L3, process(L4, len(L3)))
      

      产生

      [78, 65, 84, 69, 78, 65] [78, 65, 84, 72, 65, 78]
      

      如果您想要一个更原始的解决方案而不调用函数。您可能想计算必须重复列表的次数,并明确计算剩余部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多