0

I want to fill an array in python with ones in a specific way. An example with 4 digits is the easiest way to explain:

0000, 0001, 0010, 0100, 1000, 1001, 1010, 1100, 1101, 1110, 1111

I need a list with 8 digits, so just writing them down is not an option. I really have no idea how to do that..

Edit: To clarify the problem. I need for the first entry all digits to be 0. Then all possibilities with just one 1, then all possibilities with two 1's and so forth. The last entry would be all 1.

HighwayJohn
  • 881
  • 1
  • 9
  • 22
  • 1
    Sounds like you need to do a lot more critical thinking about the problem! This is not on-topic for Stack Overflow as written. – Adam Smith Apr 18 '16 at 23:12
  • 1
    Please note that 0010 is an octal literal and thus `8 == 0010` – robyschek Apr 18 '16 at 23:13
  • 1
    @robyschek I assume he's looking for `["00000000", "00000001", "00000010", ...]` – Adam Smith Apr 18 '16 at 23:13
  • Please see this http://stackoverflow.com/questions/699866/python-int-to-binary – OneCricketeer Apr 18 '16 at 23:18
  • @cricket_007 that doesn't really help, though, since he's not counting in binary (`0001`, `0010`, `0100` instead of `0001`, `0010`, `0011`) – Adam Smith Apr 18 '16 at 23:19
  • @Adam: You are completely correct. Binary counting would be no problem for me.. – HighwayJohn Apr 18 '16 at 23:24
  • I've written an answer, but now I see the example given is peculiar: it only has 11 numbers in it. What happened to 0011, 0101, 0110, 0111 and 1011? – Paul Hankin Apr 18 '16 at 23:30
  • @PaulHankin If you stop thinking in binary and just look at patterns, this becomes clearer! It's not binary *REALLY*. `0010` is not 2, it's just `ooxo`. There is no `ooxx` element in that pattern. – Adam Smith Apr 18 '16 at 23:34
  • At least I think so.... The edit just now has muddied those waters. It would be great if someone could close this until we had some code to look at or at least a clear problem statement, but we need another voter. – Adam Smith Apr 18 '16 at 23:35
  • @Paul: Oh you are correct Paul. That was a mistake by myself. I need all numbers. To sort the list solved my problem in principle. But I need to print an array entry: w[0][0][0][0][0][0][0][0] , w[0][0][0][0][0][0][0][1], ... and so forth. Know I don't know how to put the numbers into the array :/ – HighwayJohn Apr 18 '16 at 23:49

2 Answers2

3

An easy way is to sort all the binary representations by the number of 1s they have. Since python sort is stable, this also keeps the original order between numbers with the same number of 1s.

sorted(('{:08b}'.format(i) for i in xrange(256)), key=lambda x: x.count('1'))
Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

Its not in the same order in your example, I assume you don't need the specific order:

for i in range(256):
    print "{:08b}".format(i)

Also I assume you need values in strings, since you specified stuff like 000001 and that expression is meaningless in numbers (equals 1).

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50