首页 文章

通过组合三个不均匀长度列表中的数字来创建一个数字列表[重复]

提问于
浏览
1

这个问题在这里已有答案:

我'm trying to create a list of id',其中数字由三个不同的列表组成:数字的第一部分应该来自列表 pers

pers = list(range(1,12))

第二部分应该是人员ID和数字1或2的组合(不一定是列表,因为它只是两个数字),如下所示:

ids = [11, 12, 21, 22, 31, 32, 41, 42, 51, 52, 61, 62, 71, 72, 81, 82, 91,
       92, 101, 102, 111, 112]

第三部分应该是以上列表和 reps 列表中的值的组合:

reps = list(range(1,13))

像这样:

ids = [111, 112, 113, 114, 115, 116, 117, 118, 119, 1110, 1111, 1112,
       121, 122, 123, 124, 125, 126, 127, 128, 129, 1210, 1211, 1212 ...]

等等,一直到11212(人11,条件2,重复12) . 列表中共有大约2250个项目 .

我已经尝试了各种itertools函数,或嵌套for循环,列表和this post中的提示,但似乎没有产生我需要的东西 .

4 回答

  • 0
    pers = list(range(1,12))
    cond = [1,2]
    rep = list(range(1,13))
    
    ids = ['{}{}{}'.format(a,b,c) for a in pers for b in cond for c in rep]
    
    ids2 =[]
    for i in ids:
        ids2.append(list(i))
    print(ids2)
    
  • 1

    你可以做一个三循环列表理解:

    pers = range(1, 12)
    cond = [1, 2]
    reps = range(1, 13)
    
    ids = ["{}{}{}".format(a, b, c) for a in pers for b in cond for c in reps]
    # ['111', '112', '113', '114', '115', '116', '117', '118', '119', '1110', '1111', '1112',
    #  '121', '122', '123', '124', '125', '126', '127', '128', '129', '1210', '1211', '1212',
    #  '211', '212', '213', '214', '215', '216', '217', '218', '219', '2110', ...]
    

    或者,如果您需要它们作为整数:

    ids = [int("{}{}{}".format(a, b, c)) for a in pers for b in cond for c in reps]
    
  • 1

    每个人都会给你循环解决方案:

    what about without loop and without any external module ?

    而不是三个循环,你可以尝试:

    print(list(map(lambda x:(list(map(lambda y:(list(map(lambda s:(x,y,s),reps))),cond))),pers)))
    

    输出:

    [[[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 1, 6), (1, 1, 7), (1, 1, 8), (1, 1, 9), (1, 1, 10), (1, 1, 11), (1, 1, 12)], [(1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 2, 10), (1, 2, 11), (1, 2, 12)]], [[(2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 1, 6), (2, 1, 7), (2, 1, 8), (2, 1, 9), (2, 1, 10), (2, 1, 11), (2, 1, 12)], [(2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 2, 5), (2, 2, 6), (2, 2, 7), (2, 2, 8), (2, 2, 9), (2, 2, 10), (2, 2, 11), (2, 2, 12)]], [[(3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 1, 5), (3, 1, 6), (3, 1, 7), (3, 1, 8), (3, 1, 9), (3, 1, 10), (3, 1, 11), (3, 1, 12)], [(3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 2, 5), (3, 2, 6), (3, 2, 7), (3, 2, 8), (3, 2, 9), (3, 2, 10), (3, 2, 11), (3, 2, 12)]], [[(4, 1, 1), (4, 1, 2), (4, 1, 3), (4, 1, 4), (4, 1, 5), (4, 1, 6), (4, 1, 7), (4, 1, 8), (4, 1, 9), (4, 1, 10), (4, 1, 11), (4, 1, 12)], [(4, 2, 1), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 2, 5), (4, 2, 6), (4, 2, 7), (4, 2, 8), (4, 2, 9), (4, 2, 10), (4, 2, 11), (4, 2, 12)]], [[(5, 1, 1), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 1, 5), (5, 1, 6), (5, 1, 7), (5, 1, 8), (5, 1, 9), (5, 1, 10), (5, 1, 11), (5, 1, 12)], [(5, 2, 1), (5, 2, 2), (5, 2, 3), (5, 2, 4), (5, 2, 5), (5, 2, 6), (5, 2, 7), (5, 2, 8), (5, 2, 9), (5, 2, 10), (5, 2, 11), (5, 2, 12)]], [[(6, 1, 1), (6, 1, 2), (6, 1, 3), (6, 1, 4), (6, 1, 5), (6, 1, 6), (6, 1, 7), (6, 1, 8), (6, 1, 9), (6, 1, 10), (6, 1, 11), (6, 1, 12)], [(6, 2, 1), (6, 2, 2), (6, 2, 3), (6, 2, 4), (6, 2, 5), (6, 2, 6), (6, 2, 7), (6, 2, 8), (6, 2, 9), (6, 2, 10), (6, 2, 11), (6, 2, 12)]], [[(7, 1, 1), (7, 1, 2), (7, 1, 3), (7, 1, 4), (7, 1, 5), (7, 1, 6), (7, 1, 7), (7, 1, 8), (7, 1, 9), (7, 1, 10), (7, 1, 11), (7, 1, 12)], [(7, 2, 1), (7, 2, 2), (7, 2, 3), (7, 2, 4), (7, 2, 5), (7, 2, 6), (7, 2, 7), (7, 2, 8), (7, 2, 9), (7, 2, 10), (7, 2, 11), (7, 2, 12)]], [[(8, 1, 1), (8, 1, 2), (8, 1, 3), (8, 1, 4), (8, 1, 5), (8, 1, 6), (8, 1, 7), (8, 1, 8), (8, 1, 9), (8, 1, 10), (8, 1, 11), (8, 1, 12)], [(8, 2, 1), (8, 2, 2), (8, 2, 3), (8, 2, 4), (8, 2, 5), (8, 2, 6), (8, 2, 7), (8, 2, 8), (8, 2, 9), (8, 2, 10), (8, 2, 11), (8, 2, 12)]], [[(9, 1, 1), (9, 1, 2), (9, 1, 3), (9, 1, 4), (9, 1, 5), (9, 1, 6), (9, 1, 7), (9, 1, 8), (9, 1, 9), (9, 1, 10), (9, 1, 11), (9, 1, 12)], [(9, 2, 1), (9, 2, 2), (9, 2, 3), (9, 2, 4), (9, 2, 5), (9, 2, 6), (9, 2, 7), (9, 2, 8), (9, 2, 9), (9, 2, 10), (9, 2, 11), (9, 2, 12)]], [[(10, 1, 1), (10, 1, 2), (10, 1, 3), (10, 1, 4), (10, 1, 5), (10, 1, 6), (10, 1, 7), (10, 1, 8), (10, 1, 9), (10, 1, 10), (10, 1, 11), (10, 1, 12)], [(10, 2, 1), (10, 2, 2), (10, 2, 3), (10, 2, 4), (10, 2, 5), (10, 2, 6), (10, 2, 7), (10, 2, 8), (10, 2, 9), (10, 2, 10), (10, 2, 11), (10, 2, 12)]], [[(11, 1, 1), (11, 1, 2), (11, 1, 3), (11, 1, 4), (11, 1, 5), (11, 1, 6), (11, 1, 7), (11, 1, 8), (11, 1, 9), (11, 1, 10), (11, 1, 11), (11, 1, 12)], [(11, 2, 1), (11, 2, 2), (11, 2, 3), (11, 2, 4), (11, 2, 5), (11, 2, 6), (11, 2, 7), (11, 2, 8), (11, 2, 9), (11, 2, 10), (11, 2, 11), (11, 2, 12)]]]
    
  • 4

    您可以按照建议使用itertools.product

    import itertools
    
    pers = range(1,12)
    
    ids = [1, 2]
    
    reps = range(1,13)
    
    combinations = [int("".join(map(str, x))) for x in itertools.product(pers, ids, reps)]
    
    print(combinations[:20])
    # [111, 112, 113, 114, 115, 116, 117, 118, 119, 1110, 1111, 1112, 121, 122, 123, 124, 125, 126, 127, 128]
    

相关问题