0

You can see in my code below that I am creating a list of strings that essentially forms as many circles defined by the planes list. The issue is associated with the hover list. Essentially, if click_x and click_y are within a certain region defined by the strings then it should replace all the strings within that region but for some reason text.replace(...) isn't doing anything and by that I mean after "replacing", the hover list is still the same. All help is appreciated.

Also I can't use np.where since I get a TypeError for some reason.

import numpy as np

planes = [2, 3, 4, 5]

edge = planes[-1]
x = np.linspace(-edge, edge, 50)
y = np.linspace(-edge, edge, 50)

regions = []
hover = []
# Normal Display
for i in x:
    row = []
    text_row = []
    for j in y:

        # TODO: Make what_region function
        if np.sqrt(i ** 2 + j ** 2) < planes[0]:
            row.append(7)  # <- Arbitrary number to adjust color
            text_row.append('Region 1')

        if np.sqrt(i ** 2 + j ** 2) > planes[-1]:
            row.append(5)  # <- Arbitrary number to adjust color
            text_row.append('Region {}'.format(len(planes) + 1))

        for k in range(len(planes) - 1):
            if planes[k] < np.sqrt(i ** 2 + j ** 2) < planes[k + 1]:
                row.append(k * 3)  # <- Arbitrary number to adjust color
                text_row.append('Region {}'.format(k + 2))
    regions.append(row)
    hover.append(text_row)


# indices = np.where(np.array(hover) == "Region 1")
# for a in range(len(indices[0])):
#     hover[indices[0][a]][indices[1][a]] = ("New Region")

click_x = np.random.uniform(-planes[-1], planes[-1])
click_y = np.random.uniform(-planes[-1], planes[-1])

# Change graph on Click TODO: Not filling region correctly (MUST FIX)
if np.sqrt(click_x ** 2 + click_y ** 2) < planes[0]:
    print('1', True)
    for row_ in hover:
        for text in row_:
            print(text)
            text.replace('Region 1', 'New Region')

if np.sqrt(click_x ** 2 + click_y ** 2) > planes[-1]:
    print('2', True)
    for row_ in hover:
        for text in row_:
            print(text)
            text.replace('Region {}'.format(len(planes)+1), 'Newer Region')

for k in range(len(planes) - 1):
    if planes[k] < np.sqrt(click_x ** 2 + click_y ** 2) < planes[k + 1]:
        print('3', True)
        for row_ in hover:
            for text in row_:
                print(text)
                text.replace('Region {}'.format(k+2), 'Newest Region')

print(hover)
Sterling Butters
  • 1,024
  • 3
  • 20
  • 41

1 Answers1

0

Strings in python are immutable. This means that they cannot ever be edited, only replaced.

x = "silly sam"    
x.replace("silly", "smart") 

x is still "silly sam" because the replaced version of the string was not assigned to a variable and therefore discarded.

x = "silly sam"
x = x.replace("silly", "smart")

Now x has the value "smart"

gahooa
  • 131,293
  • 12
  • 98
  • 101