![]() |
Change elements of array based on position of input data - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Change elements of array based on position of input data (/thread-37147.html) |
Change elements of array based on position of input data - Cola_Reb - May-05-2022 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:(...) 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], 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? RE: Change elements of array based on position of input data - ibreeden - May-05-2022 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.
Try to write the code for this and please let us know if it works. RE: Change elements of array based on position of input data - deanhystad - May-05-2022 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))
RE: Change elements of array based on position of input data - Cola_Reb - May-06-2022 (May-05-2022, 05:15 PM)ibreeden Wrote: That is easy. 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: The method basically works for me ![]() Quote:with open("input.txt", "r") as f: 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], RE: Change elements of array based on position of input data - Cola_Reb - May-07-2022 (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)) Thank you, quite an elegant solution. My only problem is that I have to read in multiple inputs at once like: Quote:data= [ Is there a way to get this in an easy way? Quote:array([[ True, False, True, True, True, False], Thank you again! RE: Change elements of array based on position of input data - deanhystad - May-08-2022 So what does your file look like? You've posted two very different formats. I made a file that looks like this data.txt 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:]))
RE: Change elements of array based on position of input data - Cola_Reb - May-13-2022 Ah now I got it. Thanks! ![]() |