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)
#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


Messages In This Thread
RE: How to make this function general to create binary numbers? (many nested for loops) - by bowlofred - Jun-23-2020, 03:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial issues with proper loops and binary jttolleson 16 2,703 Nov-02-2023, 08:39 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 647 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,489 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  reduce nested for-loops Phaze90 11 1,980 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  python create function validation mg24 1 871 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 819 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,576 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Create a function for writing to SQL data to csv mg24 4 1,219 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 974 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,753 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