Python Forum
Change elements of array based on position of input data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change elements of array based on position of input data
#6
So what does your file look like? You've posted two very different formats.

I made a file that looks like this
data.txt
Output:
data= [ 4 1 3 4 5 2 2 5 1 6 ]
And processed it using this code.
def bit_flags(bits, on=True, off=False):
    return [on if bit in bits else off for bit in range(1, max(bits)+1)]

with open("data.txt", "r") as file:
    next(file)  # Skip data[
    for line in file:
        if len(line) > 1:  # Skip ]
            numbers = list(map(int, line.split()))
            print(numbers, bit_flags(numbers[1:]))
Output:
[4, 1, 3, 4, 5] [True, False, True, True, True] [2, 2, 5] [False, True, False, False, True] [1, 6] [False, False, False, False, False, True]
Cola_Reb likes this post
Reply


Messages In This Thread
RE: Change elements of array based on position of input data - by deanhystad - May-08-2022, 03:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Changing client.get() method type based on size of data... dl0dth 1 716 Jan-02-2025, 08:30 PM
Last Post: dl0dth
  I think I need to delete input data because returning to start fails thelad 2 1,095 Sep-24-2024, 10:12 AM
Last Post: thelad
  ValueError: could not broadcast input array from shape makingwithheld 1 2,482 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,431 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,665 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  unable to remove all elements from list based on a condition sg_python 3 1,692 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 12,009 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  manually input data jpatierno 0 780 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 2,676 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,961 Aug-31-2023, 05:36 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