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.
  • 53
  • 1
  • 10
  • Check [this](http://stackoverflow.com/questions/252703/python-append-vs-extend) out. I haven't fully analized your code but I believe your problem resides in `append()`. Try to replace it for `extend()`. – kazbeel May 18 '16 at 12:29
  • Excellent, that totally worked! Thank you very much! – Veligkekas G. May 18 '16 at 12:32
  • Glad to hear that @Veligkekas G. – kazbeel May 18 '16 at 12:33
  • If one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. See [here](http://meta.stackexchange.com/a/5235) for a full explanation. – PM 2Ring May 18 '16 at 14:12
  • I didn't know that, I just did it :) Thank you @PM2Ring – Veligkekas G. May 19 '16 at 13:23

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,328
  • 17
  • 37
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
  • 317
  • 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,857
  • 8
  • 16
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
  • 52,226
  • 5
  • 73
  • 162