3

Trying to print possible number combinations in single line as list but the list have wrong output. My output is like this:

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

When it should be like this:

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

My code is

if __name__ == '__main__':
    x = 1
    y = 1
    z = 1 
kordinat = ["x","y","z"]
result = []
for xx in range(x+1):
    kordinat[0] = xx
    for yy in range(y+1):
        kordinat[1] = yy
        for zz in range(z+1):
            kordinat[2]= zz
            print(kordinat)
            result.append(kordinat)
print(result)
Haem
  • 929
  • 6
  • 15
  • 31
  • 1
    Are you trying to print elements of a list in a new line? – shaik moeed Oct 14 '19 at 07:14
  • i want output in input(result) to be not only 1's but having different values like this: [0, 0, 0] [0, 0, 1] [0, 1, 0] [0, 1, 1] [1, 0, 0] [1, 0, 1] [1, 1, 0] [1, 1, 1] – Lütfullah Erkaya Oct 14 '19 at 07:23
  • You seem to be asking two different questions: 1. How to output the list in multiple lines and 2) how to construct the list in the first place. You need to post two different questions for that. – Haem Oct 14 '19 at 07:25
  • @satyamsoni You should post that as an answer. – Haem Oct 14 '19 at 07:28
  • to print 1 sublist per line, you could use `print('\n'.join([str(r) for r in result]))` or more efficient simply `for r in result: print(r)` – FObersteiner Oct 14 '19 at 07:37

4 Answers4

6

You should take itertools.product():

from itertools import product

result = list(product(range(2), repeat=3))
print(result)
# [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

EDIT: This does not explain, why you end up having a list full of [1, 1, 1] elements. The reason can be found here. Through all your iterations, you work with a list called koordinat. When you append this to your overall list result, you append a reference to the very same object all the time. So you will have a list full of references to the same list koordinat. So changing koordinat to [1, 1, 1] in the last iteration, will change all references in your result list to this value as well. This can best be seen, when you print result after each append()

AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33
3

change this line:

result.append(kordinat)

to

result.append(kordinat.copy())

list is passed or assigned as reference so if you change the value it'll change everywhere.

Krishna
  • 6,107
  • 2
  • 40
  • 43
1

To print elements of list in new line use pprint as below,

>>> from pprint import pprint as pp
>>> pp(result)
[[1, 1, 1],
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1]]

Edit-1:

Assuming, you are trying to find the binary numbers of the sequence. In your example, it is from 0-7(1-byte or 8-bits).

Try this,

>>> result = [[int(j) for j in "{0:03b}".format(i)] for i in range(8)]
>>> pp(result)
[[0, 0, 0],
 [0, 0, 1],
 [0, 1, 0],
 [0, 1, 1],
 [1, 0, 0],
 [1, 0, 1],
 [1, 1, 0],
 [1, 1, 1]]
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • ty for answering but i don't want the result to be all 1's. i want it to have different values like this: [0, 0, 0] [0, 0, 1] [0, 1, 0] [0, 1, 1] [1, 0, 0] [1, 0, 1] [1, 1, 0] [1, 1, 1]. But I can't understand why it only results 1's. – Lütfullah Erkaya Oct 14 '19 at 07:21
  • 1
    @LütfullahErkaya Have you read my answer? I gave explanation on why that happens and also provided a link where this behavioir is explained in detail. Also the answer of krishna points into this direction! – AnsFourtyTwo Oct 14 '19 at 09:40
  • Yeah, i read it and now i see the problem. ty – Lütfullah Erkaya Oct 15 '19 at 10:41
1

If you want all the possible combinations of 0,1 of size 3, use combinations from itertools and call it as combinations([0,1],3). This will give you all the possible combinations you are expecting

satyam soni
  • 259
  • 1
  • 9
  • `list(combinations([0,1], 3))` gives an empty array. I am not sure why, though. – AnsFourtyTwo Oct 14 '19 at 07:38
  • Use this ` from itertools import product as p print(list(p(range(2),repeat=3))) [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] ` – satyam soni Oct 14 '19 at 07:48