1

I'm trying to create a list of id's, where the numbers are made up of three different lists: the first part of the number should be from the list pers:

pers = list(range(1,12))

The second part should be a combination of the person id, and the number 1 or 2 (not necessarily a list, as it is just two numbers), like this:

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

The third part should be a combination of the above and the values in the list reps:

reps = list(range(1,13))

like this:

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

and so on, all the way up to 11212 (person 11, condition 2, repetition 12). In total around 2250 items in the list.

I've tried various itertools functions, or nested for loops, lists of lists and tips in this post, but none seem to yield what I need.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
E. V.
  • 43
  • 1
  • 7
  • 1
    Can you show your attempt that got closest to what you're looking for? – glibdud Dec 08 '17 at 13:52
  • Note that your list may contain duplicates produced different ways; for example 1111 can be produced 3 distinct ways: (1,1,11), (1,11,1) and (11,1,1). – Scott Hunter Dec 08 '17 at 15:32

4 Answers4

4

You can do a triple loop list comprehension:

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', ...]

Or, if you need them as integers:

ids = [int("{}{}{}".format(a, b, c)) for a in pers for b in cond for c in reps]
zwer
  • 24,943
  • 3
  • 48
  • 66
1

Everyone is going to give you loop solution :

what about without loop and without any external module ?

instead of three loops you can just try:

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

output:

[[[(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)]]]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
1

You can just use itertools.product as you suggested:

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]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • This is interesting, do you know how this compares in terms of performance to the tripple list comprehension method proposed by @zwer? – E. V. Dec 13 '17 at 10:10
  • @E.V. - in general, `itertools` facilities will be usually a bit slower than doing it yourself because, internally, they do the same thing. Even if you ditch the slow `join()/map()` combo and use the formatting facilities (e.g. `[int("{}{}{}".format(*x)) for x in itertools.product(pers, cond, reps)]` ) it will still end up a hair slower than a manual triple loop, but then the speed difference would be so negligible to make it a matter of personal taste - I prefer the explicit approach, but nothing wrong with the convenience of `itertools`. – zwer Dec 13 '17 at 10:29
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)
Spox
  • 11
  • 2
  • 1
    Whilst code only answers may solve the original problem, some explanation of your solution would improve the quality of this answer. – Nigel Ren Dec 08 '17 at 15:13