0

I'm having troubles writing this piece of code. I need to create a list to only have 3 values every 3 values :

The expected output must be something like :

output1 = [1,2,3,7,8,9,13,14,15,....67,68,69]
output2 = [4,5,6,10,11,12...70,71,72]

Any ideas how can I reach that ?

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
ssjss7
  • 1
  • 4

3 Answers3

1

Use two loops -- one for each group of three, and one for each item within that group. For example:

>>> [i*6 + j for i in range(12) for j in range(1, 4)]
[1, 2, 3, 7, 8, 9, 13, 14, 15, 19, 20, 21, 25, 26, 27, 31, 32, 33, 37, 38, 39, 43, 44, 45, 49, 50, 51, 55, 56, 57, 61, 62, 63, 67, 68, 69]
>>> [i*6 + j for i in range(12) for j in range(4, 7)]
[4, 5, 6, 10, 11, 12, 16, 17, 18, 22, 23, 24, 28, 29, 30, 34, 35, 36, 40, 41, 42, 46, 47, 48, 52, 53, 54, 58, 59, 60, 64, 65, 66, 70, 71, 72]
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

To make the logic understandable, because sometimes the Pythonic methods look 'magic' Here's a naive algorithm to do that:

output1 = []
output2 = []
for i in range(1, 100):  # change as you like:
    if (i-1) % 6 < 3:
        output1.append(i)
    else:
        output2.append(i)

What's going on here:

  • Initializing two empty lists.
  • Iterate through integers in a range.
  • How to tell if i should go to output1 or output2:
    • I can see that 3 consecutive numbers go to output1, then 3 consecutive to output2.
    • This tells me I can use the modulo % operator, (doing % 6)
    • The rest is simple logic to get the exact result wanted.
eroot163pi
  • 1,791
  • 1
  • 11
  • 23
aydee45
  • 516
  • 3
  • 14
  • This is not the pythonic way of accomplishing this task. Most of the time, if you find yourself using `for i in range()` and then indexing with the list, you need to re-visit your approach. Consider using list comprehension instead. – blackbrandt Aug 26 '21 at 14:27
  • Downvote wasn't me. However, if you want to make the code readable, consider using more pythonic approaches. – blackbrandt Aug 26 '21 at 14:31
  • I probably would have written myself something else, or maybe even snooped around here to find something very Pythonic. However, this seems to me like a beginner's task, and I'm looking to make the logic understandable, because sometimes the Pythonic methods look 'magic'. Overall agree with your input. – aydee45 Aug 26 '21 at 14:35
0

Suppose you want n values every n values of total sets starting with start. Just change the start and number of sets you need. In below example list start with 1, so first set [1,2,3] and we need 12 sets each containing 3 consecutive element

Method 1

n = 3
start = 1
total = 12

# 2*n*i + start is first element of every set of n tuples (Arithmetic progression)
print([j for i in range(total) for j in range(2*n*i + start, 2*n*i + start+n)])

# Or
print(sum([list(range(2*n*i + start, 2*n*i + start+n)) for i in range(total)], []))

Method 2 (Numpy does operation in C, so fast)

import numpy as np

n = 3
start = 1
total = 12

# One liner
print(
    (np.arange(start, start + n, step=1)[:, np.newaxis] + np.arange(0, total, 1) * 2*n).transpose().reshape(-1)
)


##############EXPLAINATION OF ABOVE ONE LINEAR########################

# np.arange start, start+1, ... start + n - 1
first_set = np.arange(start, start + n, step=1)
# [1 2 3]

# np.arange 0, 2*n, 4*n, 6*n, ....
multiple_to_add = np.arange(0, total, 1) * 2*n
print(multiple_to_add)

# broadcast first set using np.newaxis and repeatively add to each element in multiple_to_add
each_set_as_col = first_set[:, np.newaxis] + multiple_to_add
# [[ 1  7 13 19 25 31 37 43 49 55 61 67]
#  [ 2  8 14 20 26 32 38 44 50 56 62 68]
#  [ 3  9 15 21 27 33 39 45 51 57 63 69]]

# invert rows and columns
each_set_as_row = each_set_as_col.transpose()
# [[ 1  2  3]
#  [ 7  8  9]
#  [13 14 15]
#  [19 20 21]
#  [25 26 27]
#  [31 32 33]
#  [37 38 39]
#  [43 44 45]
#  [49 50 51]
#  [55 56 57]
#  [61 62 63]
#  [67 68 69]]

merge_all_set_in_single_row = each_set_as_row.reshape(-1)
# array([ 1,  2,  3,  7,  8,  9, 13, 14, 15, 19, 20, 21, 25, 26, 27, 31, 32,
#        33, 37, 38, 39, 43, 44, 45, 49, 50, 51, 55, 56, 57, 61, 62, 63, 67,
#        68, 69])

eroot163pi
  • 1,791
  • 1
  • 11
  • 23