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
#1
Question 
Hey everyone, hope you are doing great these days. I have a difficult problem with my data processing and hope you could help me:

I have some input data of the following form: Number of entries to come, Entries...

Example:

Quote:(...)

4 1 3 4 5

2 2 5

1 6

(...)

At the same time I have an array with only zero/false entries and now I always need a true/1 entry at the position corresponding to the entries of the first array. Example:

Quote:array([[True, False, True, True, True, False],

[False, True, False, False, True, False],

[False, False, False, False, False, True]])


Is there a smooth way to iterate over my input data and add a True/1 to the second array at the corresponding position in the array below?
Reply
#2
That is easy.
From your examples I guess it is all about the numbers 1 to 6. Is it a dice-game? The easyest way is as follows.
  1. Let's call your input string "input_string". Turn it to a list with split. (input_list = input_string.split())
  2. Create a list of 6 booleans, initial False. (boolean_list = [False] * (6+1). "+ 1" is because lists count from 0 and you count from 1. The zeroth item will be unused. That is a cosmetic flaw. We can discard it later.)
  3. Iterate over the input_list but make a slice of it to start with item 1. (for input_item in input_list[1:]:)
  4. Set the corresponding item in boolean_list to True. (boolean_list[int(input_item)] = True])
  5. Append the resulting boolean list to your array. Make a slice of it to discard the zeroth item. (boolean_list[1:])

Try to write the code for this and please let us know if it works.
Cola_Reb likes this post
Reply
#3
def bit_flags(bits, on=True, off=False):
    return [on if bit in bits else off for bit in range(1, max(bits)+1)]


bits = list(map(int, input("Enter values preceeded by count: ").split()))
print(bits[1:], bit_flags(bits[1:]))
print(bits[1:], bit_flags(bits[1:], 1, 0))
Output:
>>> 4 1 3 4 5 [1, 3, 4, 5] [True, False, True, True, True] [1, 3, 4, 5] [1, 0, 1, 1, 1] >>> 1 6 [6] [False, False, False, False, False, True] [6] [0, 0, 0, 0, 0, 1]
Cola_Reb likes this post
Reply
#4
(May-05-2022, 05:15 PM)ibreeden Wrote: That is easy.
From your examples I guess it is all about the numbers 1 to 6. Is it a dice-game? The easyest way is as follows.

Thank you very much for your solution approach! Unfortunately it is not a dice game but a little more complex dataset with up to 3000 values but for my example I wanted to keep it as simple as possible.

(May-05-2022, 05:15 PM)ibreeden Wrote:
  1. Let's call your input string "input_string". Turn it to a list with split. (input_list = input_string.split())
  2. Create a list of 6 booleans, initial False. (boolean_list = [False] * (6+1). "+ 1" is because lists count from 0 and you count from 1. The zeroth item will be unused. That is a cosmetic flaw. We can discard it later.)
  3. Iterate over the input_list but make a slice of it to start with item 1. (for input_item in input_list[1:]:)
  4. Set the corresponding item in boolean_list to True. (boolean_list[int(input_item)] = True])
  5. Append the resulting boolean list to your array. Make a slice of it to discard the zeroth item. (boolean_list[1:])

The method basically works for me Smile . In the end, however, I get a long list of true/false entries. The problem is that I didn't explain very well what my input data looks like: I'm reading a *.txt file where each line should be a separate element in the boolean array at the end:

Quote:with open("input.txt", "r") as f:
data = f.readlines()

data= [' Headline \n',
' 4 1 3 4 5 \n',
' 2 2 5 \n',
' 1 6 \n',
....]

So basically I need to differentiate between the rows in the input data in a way that they could be found in the results separately:

Quote:array([[ True, False, True, True, True, False],

[False, True, False, False, True[/color], False],

[False, False, False, False, False, True]])
Reply
#5
(May-05-2022, 05:28 PM)deanhystad Wrote:
def bit_flags(bits, on=True, off=False):
    return [on if bit in bits else off for bit in range(1, max(bits)+1)]


bits = list(map(int, input("Enter values preceeded by count: ").split()))
print(bits[1:], bit_flags(bits[1:]))
print(bits[1:], bit_flags(bits[1:], 1, 0))
Output:
>>> 4 1 3 4 5 [1, 3, 4, 5] [True, False, True, True, True] [1, 3, 4, 5] [1, 0, 1, 1, 1] >>> 1 6 [6] [False, False, False, False, False, True] [6] [0, 0, 0, 0, 0, 1]

Thank you, quite an elegant solution. My only problem is that I have to read in multiple inputs at once like:

Quote:data= [
4 1 3 4 5
2 2 5
1 6
....]

Is there a way to get this in an easy way?

Quote:array([[ True, False, True, True, True, False],

[False, True, False, False, True[/color], False],

[False, False, False, False, False, True]])


Thank you again!
Reply
#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
#7
Ah now I got it. Thanks! Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 986 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  manually input data jpatierno 0 316 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 913 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 973 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Grouping Data based on 30% bracket purnima1 4 1,142 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Change a numpy array to a dataframe Led_Zeppelin 3 1,066 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,912 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  conditionals based on data frame mbrown009 1 873 Aug-12-2022, 08:18 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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