Python Forum
Convert list of integers to bit-mapped byte
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert list of integers to bit-mapped byte
#2
Quote:Is there a Python library that will handle this for me
Luckily, this isn't javascript.  We don't have a repository of functions that are only two lines each.


Try this: create a list of 8 items, and set all of them to zero.  Each item in your list of solenoids you want on, is then the index, so just set it to one.

Something like this:
>>> def bitmap(on_solenoids):
...   bits = [0 for _ in range(8)]
...   for solenoid in on_solenoids:
...     bits[solenoid] = 1
...   return bits
...
>>> bitmap([1, 3, 4, 7])
[0, 1, 0, 0, 1, 1, 0, 1]
Reply


Messages In This Thread
RE: Convert list of integers to bit-mapped byte - by nilamo - Oct-24-2017, 09:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,268 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Timestamp of file changes if a share mapped to a server…. tester_V 34 4,148 Jul-04-2023, 05:19 AM
Last Post: tester_V
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,536 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,518 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,994 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Error "list indices must be integers or slices, not str" dee 2 1,509 Dec-30-2022, 05:38 PM
Last Post: dee
  convert a list to links Pir8Radio 3 1,149 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,562 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  read a text file, find all integers, append to list oldtrafford 12 3,740 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Convert list to interger Clives 5 1,671 May-09-2022, 12:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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