Python Forum
How to make this function general to create binary numbers? (many nested for loops)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make this function general to create binary numbers? (many nested for loops)
#1
As you can notice, this function is designed to create binary numbers in order into a list of lists. It works for 4 digits and each for loop is responsible for modifying a certain position.

I would like to modify it so I can use it for any number of digits, but I haven't found a way to do it as I have to add a for loop every time I want to add a digit.

Thank you so much for your help.

def Binarynum():
    lista=[];
    for i in range(2):
        for j in range(2):
            for k in range(2): 
                for l in range(2):
                    lista.append([i,j,k,l]);
    return lista;

lista=Binarynum();
Reply
#2
Do not use a semicolon in Python code - it isn't used at the end of every line
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
You could use the product function from itertools.

>>> from itertools import product
>>> list(product([0, 1], repeat=4))
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]
Just change the repeat value.
Reply
#4
(Jun-23-2020, 03:51 PM)pyzyx3qwerty Wrote: Do not use a semicolon in Python code - it isn't used at the end of every line
I know, I just prefer to do it because every other language I code in, requires it. So I do it anyway.
Reply
#5
How about a function that converts positive integers to lists of bits.
def binary(number):
    bits = []
    while number != 0:
        bits.insert(0, number % 2)
        number = number >> 1
    return bits
If you want to pad to some fixed length.
def binary(number, pad=0):
    bits = []
    while number != 0:
        bits.insert(0, number % 2)
        number = number >> 1
    while len(bits) < pad:
        bits.insert(0, 0)
    return bits
You could also use bin() and a list comprehension:
def binary(number, pad=0):
    return [1 if bit == '1' else 0 for bit in bin(number)[2:]]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial issues with proper loops and binary jttolleson 16 2,463 Nov-02-2023, 08:39 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,362 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  reduce nested for-loops Phaze90 11 1,757 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  python create function validation mg24 1 807 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 757 Nov-09-2022, 01:50 PM
Last Post: korenron
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,428 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Create a function for writing to SQL data to csv mg24 4 1,110 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 905 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,636 Aug-19-2022, 11:07 AM
Last Post: dm222

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020