列表项从长度为4的列表B到列表A的每个列表项的映射

时间:2019-03-20 09:34:45

标签: python list loops nested

我在解决以下问题时遇到问题,请检查以下代码。

filenamee5 = 'ABC'
counter1 = 1
counter2 = 2          
list1 = []
list2 = range(1000)
for x in list2:
    counter1 = str(counter1)
    full_name5 = (filenamee5 + counter1)
    list1.append(full_name5)
    counter1 = counter2
    counter2 += 1
    numbers_list = []
    level1 = []
    for x in list1:
        numbers_list.append(x)
        ListsOfAll = numbers_list[1:1000]
        for x1 in range(1, len(ListsOfAll), 4):
            level1.append(list(numbers_list[x1:x1 + 4]))
            l1 = level1[0]
            l2 = level1[1:5]
            l3 = level1[5:21]
            l4 = level1[21:85]
            l5 = level1[85:341]
            l6 = level1[341:1365]
            ll2 = []
            for i in range(len(l2)):
                aaa = l1[i], l2[i]
                ll2.append(aaa)
                print(ll2)
                print("the length is:",len(ll2))

我仅对第一和第二个列表的实际输出:

[('ABC2', ['ABC6', 'ABC7', 'ABC8', 'ABC9']),

 ('ABC3', ['ABC10', 'ABC11', 'ABC12', 'ABC13']),

 ('ABC4', ['ABC14', 'ABC15', 'ABC16', 'ABC17']), 

 ('ABC5', ['ABC18', 'ABC19', 'ABC20', 'ABC21'])]

the length is: 4

此处每个列表项应包含以下列表中的四个项,即1:4比例。

请参见下面的示例以了解我的问题:

List1 = ['A','B','C','D']

List2=['E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T']

List3 = ['A',['E','F','G','H'],'B',['I','J','K','L'],'C',['M','N','O','P'],'D',['Q','R','S','T']]

请帮助我提供您的建议,将不胜感激!

1 个答案:

答案 0 :(得分:0)

我根本不知道您为什么需要这个(我敢猜想这个可能xy problem ...的另一种情况...),我必须声明这是将一个问题的不同样本数据集添加到一篇文章中有点奇怪...但是-也许您可以使用以下代码:
(我称其为“二次链接”,因为mapping有所不同...)

解决方案功能:

def quadraticChaining(somelists):
    out = []
    for i, e in enumerate(somelists[0]):
        out.append(e)
        for p, sub in enumerate(somelists[1:]):
            L = 4**(p+1)
            out.append(sub[i*L:(i+1)*L])
    return out

...或者如果您不需要整个内存,而只想遍历它
作为生成器:

def quadraticChaining(somelists):
    for i, e in enumerate(somelists[0]):
        yield e
        for p, sub in enumerate(somelists[1:]):
            L = 4**(p+1)
            yield sub[i*L:(i+1)*L]

样本数据:

list1 = ['A'+str(i) for i in range(4)]
list2 = ['B'+str(i) for i in range(4**2)]
list3 = ['C'+str(i) for i in range(4**3)]
list4 = ['D'+str(i) for i in range(4**4)]
list5 = ['E'+str(i) for i in range(4**5)]
list6 = ['F'+str(i) for i in range(4**6)]

功能示例:

quadraticChaining([list1, list2])

# ['A0',
#  ['B0', 'B1', 'B2', 'B3'],
#  'A1',
#  ['B4', 'B5', 'B6', 'B7'],
#  'A2',
#  ['B8', 'B9', 'B10', 'B11'],
#  'A3',
#  ['B12', 'B13', 'B14', 'B15']]

quadraticChaining([list1, list2, list3])

# ['A0',
#  ['B0', 'B1', 'B2', 'B3'],
#  ['C0',
#   'C1',
#   'C2',
#   'C3',
#   'C4',
#   'C5',
#   'C6',
#   'C7',
#   'C8',
#   'C9',
#   'C10',
#   'C11',
#   'C12',
#   'C13',
#   'C14',
#   'C15'],
#  'A1',
#  ['B4', 'B5', 'B6', 'B7'],
#  ['C16',
#   'C17',
#   'C18',
#   'C19',
#   'C20',
#   'C21',
#   'C22',
#   'C23',
#   'C24',
#   'C25',
#   'C26',
#   'C27',
#   'C28',
#   'C29',
#   'C30',
#   'C31'],
#  'A2',
#  ['B8', 'B9', 'B10', 'B11'],
#  ['C32',
#   'C33',
#   'C34',
#   'C35',
#   'C36',
#   'C37',
#   'C38',
#   'C39',
#   'C40',
#   'C41',
#   'C42',
#   'C43',
#   'C44',
#   'C45',
#   'C46',
#   'C47'],
#  'A3',
#  ['B12', 'B13', 'B14', 'B15'],
#  ['C48',
#   'C49',
#   'C50',
#   'C51',
#   'C52',
#   'C53',
#   'C54',
#   'C55',
#   'C56',
#   'C57',
#   'C58',
#   'C59',
#   'C60',
#   'C61',
#   'C62',
#   'C63']]

发电机示例:

L = quadraticChaining([list1, list2, list3])
# <generator object quadraticChaining at 0x0000000009993B88>

next(L)
# 'A0'

next(L)
# ['B0', 'B1', 'B2', 'B3']

next(L)
# ['C0',
#  'C1',
#  'C2',
#  'C3',
#  'C4',
#  'C5',
#  'C6',
#  'C7',
#  'C8',
#  'C9',
#  'C10',
#  'C11',
#  'C12',
#  'C13',
#  'C14',
#  'C15']

next(L)
# 'A1'

...