1

Good evening! I have the following code which works when you write python new.py -s 13 -p 5 on the command prompt.

What command prompt prints is : [[1, [0], [0]], [1, [0], [0]], [1, [0], [0]], [1, [0]], [1, [0]]]

But I wanted: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0], [1, 0]]

How can I do this?

-s 12 is the length of the string and -p 7 the 1s.

Thank you!

My code sample :

import argparse


p = argparse.ArgumentParser()
p.add_argument("-pulses", help = "number of pulses", type = int)
p.add_argument("-slots", help = "length of the rythm", type = int)
args = p.parse_args()
slots = args.slots
pulses = args.pulses


pauses = slots - pulses

mod = slots % pulses

rhythm = []

if mod != 0:
    i = 0
    j = 0
    temp = []

    while i < pulses:
        rhythm.append([1])
        i = i + 1

    while j < pauses:
        rhythm.append([0])
        j = j + 1

    m = slots
    n = pauses

    while (rhythm[-1]==[0]):

        if (n!=0):
            step = m%n
            hlp = n
            m = n
            n = step

            i = 0

            while (i<step):
                rhythm[i].append(rhythm[-1])
                rhythm.remove(rhythm[-1])
                i = i + 1

print (rhythm)
Veligkekas G.
  • 43
  • 1
  • 10

4 Answers4

3

Note: This is a mere copy&paste from the comment.

Check this out.

I haven't fully analyzed your code but I believe your problem resides in append(). Try to replace it for extend().

Community
  • 1
  • 1
kazbeel
  • 1,378
  • 19
  • 40
0

Woozy Coder definitively correctly answered this question. More generally speaking about lists appending and extending:

  • dest_list.append(appended_item) appends an item to the list. Should the appended item be a list, this list will be appended as is, and becomes an additional item at the end of the destination list.

  • dest_list.extend(list_extension) extends a list with another list, every item of this other list being individually appended to the end of the destination list.

Schmouk
  • 327
  • 2
  • 6
0

The Problem is this line

rhythm[i].append(rhythm[-1])

rhythm[-1] returns a list ([0] or [1]). So you must use extend instead of append.

rhythm[i].extend(rhythm[-1])

Python List

qvpham
  • 1,896
  • 9
  • 17
0

Your code seems overly complicated. I think the code below does what you're trying to do:

def rhythm_list(slots, pulses):
    q, r = divmod(slots, pulses)
    full = [1] + [0] * q
    part = full[:-1]
    return [full] * r + [part] * (pulses - r)

# Test
print(rhythm_list(13, 5))
print(rhythm_list(12, 7))
print(rhythm_list(12, 4))

output

[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0], [1, 0]]
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1], [1]]
[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]

Note that the output list contains duplicated references to the full and part lists, so if you do:

m = rhythm_list(12, 7)
m[0][0]=2

then m becomes:

[[2, 0], [2, 0], [2, 0], [2, 0], [2, 0], [1], [1]]

If this behaviour is undesirable, then change the last line of rhythm_list to

return [full[:] for _ in range(r)] + [part[:] for _ in range(pulses - r)]
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182