script question

How would I find a string in a list of lists with the character ‘<’ then remove that character?

Ex:

ExList = [[‘Jeff’, ‘Tom<’],[‘Steve’],[],[],[]]

Find ‘Tom<’ and make it ‘Tom’

Thanks.


for subList in ExList :
    for item in subList:
        if item == "Tom&lt;":
            item = item.replace('&lt;', "")

@Nicholas_A

you sure this works? (i dont know if you can change the string in the list like that)

(if Nicholas_A’s Solution doesnt work) i would do:


ExList = [['Jeff', 'Tom&lt;'],['Steve'],[],[],[]]

find_and_delete = "&lt;"
for listindex in range(len(ExList)):
templist = ExList
[listindex] #this should be in one line, dont know why it istn

for sindex in range(len(templist)):

[INDENT=2]mystring = templist[sindex]
if not find_and_delete in mystring:[/INDENT]
[INDENT=3]continue[/INDENT]
[INDENT=2]mystring = mystring.replace(find_and_delete,"")
templist[sindex] = mystring
[/INDENT]


untested


ExList = [['Jeff', 'Tom&lt;'],['Steve'],[],[],[]]

for inner in ExList:
    for ix,word in enumerate(inner):
        if "&lt;" in word:
            inner[ix] = word.strip("&lt;")

generally you can edit the item in place as long as you’re not changing the length of the list while iterating it

@Leralass:
Lists are mutable - you can change them in-place:

some_list = [2, 4, 5, 6]
some_list[1] = 5
print(some_list) # [2, 5, 5, 6]

@J05HU4:
It wouldn’t suprise me if there was a better way of storing this than a lists of lists. And how did the “>” get there in the first place. We can solve the problem you ask us about (how to remove the “>”), but it is likely there is a better solution that solves the real issue.

@sdgeoff
yes lists are mutable but strings aren’t.

so here he changed a string from the list and not the list itself (that’s why i wasn’t sure if that works)

(he assigned the string a name [= copied it from the list - at least that’s how i understand it] and then changed it)

ok i tested it and it doesn’t work.


ExList = [["Tom&lt;"],["Bot"],[]]


for subList in ExList:
for item in subList:
[INDENT=2] if item == "Tom&lt;": #item == string == a copy from the list == immutable[/INDENT]
[INDENT=3]item = item.replace('&lt;', "")
[/INDENT]

print(ExList) #prints: [["Tom&lt;"],["Bot"],[]]

Ahh, you’re right, I assumed he had done the necessary re-assignment:


for subList in ExList :
    for item_id, item in enumerate(subList):
        if item == "Tom&lt;":  # Can be removed
            subList[item_id] = item.replace('&lt;', "")

Oh I didn’t know he wanted the list item to be changed too cause he didn’t explicitly say that the item in the list had to be changed.

Thanks guys, it works, sorry I wasn’t clear enough.

@Sdfgeoff the reason the ‘<’ is there is because I have a menu with names in it and the ‘<’ is showing that the name is selected.

One more thing, could someone explain to me what this means:

for whatever, something in enumerate(stuff)

I’ve seen it a lot but I don’t get it.

https://docs.python.org/2/library/functions.html#enumerate

you get the index and the list[index]

OK thanks, what about the first part?



mylist = ["asd","fgd","bot","bob"]

for x,y in enumerate(mylist):#x is the index == integer
#y = mylist[index] == string (in this example)
pass


#OR

for mytuple in enumerate(mylist):
#mytuple is a tuple of: (index, mylist[ index ] )
pass


#x,y explanation:
tuple2 = (1,2,3,4) # a tuple is a list, but you cannot change items, so tuple2[1] = 3 wont work

a,b,c,d = tuple2
#a = 1
#b = 2
#c = 3
#d = 4

#also works with lists


What that does is, the first variable, “whatever”, in your case, will be the counter. It gives you the index of the current iteration of the list, in your case, of “something”. Say for instance, you do:


for count, item in enumerate( ["hello", "my", "name", "is", "nick"] ):
    print(count, item)

and you will get:


0 hello
1 my
2 name
3 is
4 nick

So enumerate returns two items. And in python you can do:


a, b = [1, 2]
print(a)  # 1
print(b)  #2

So when you do:


for id, item in enumerate(some_list):

You get the item’s position in the list in the variable “id” and the item itself.

@Sdfgeoff the reason the ‘<’ is there is because I have a menu with names in it and the ‘<’ is showing that the name is selected
.
May I suggest separating out which one is highlighted from the “text” that is displayed.
How I would handle this is:


MENU_ITEMS= [
    'start',
    'options',
    'help',
    'quit'
]

def prepare_for_display(menu_items, selected_id):
    out_str = ''
    for item_id, item in enumerate(menu_items):
        if item_id == selected_id:
            out_str += item + '&lt;'
        else:
            out_str += item
        out_str += '
'
    return out_str

print(prepare_for_display(MENU_ITEMS, 0))
print(prepare_for_display(MENU_ITEMS, 3))
print(prepare_for_display(MENU_ITEMS, 2))

This is a small example of “MVC” (Model View Control) where you separate the model (which one you have selected) from the view (the out_str). The Control is not in that example.

Now evidently your menu system has more than one layer, in which case you will need a slightly more complex “prepare_for_display” function, but the key takeawa is that the “MENU_ITEMS” list is never modified as part of displaying the menu. Obviously if your menu is dynamic (has changing items) then that needs to be handled somewhere as well, but not as part of displaying the menu.

well that happens if 3 people simultaneously write xD

Haha, thanks everyone!

You are now leaving the area where the list of list of names is sufficient.
You are going to store other information then names inside this data structure. As you should already noticed: any new operation gets harder and harder. Imagine you have additional selection methods.

Why you modify the name of a person because it is selected by something. You do not change your name because you are selected . It is the other way around: The other (who selected you) knows you are selected. So he is referring to you.

Yes, you can let the selected object know it is selected, but coding it into a name is not a good choice. Imagine the name already contains a “<”. Beside of that it is unnecessary complicate.

For selection I suggest to have a selection object, that either refers to the selected object


selectedObject = "Tom"

or it contains enough information for an efficient access.


selectedAgeIndexs = 0
selectedNameIndex = 1

...
getSelectedName():
   return ExList[selectedAgeIndexs, selectedNameIndex]


Why you modify the name of a person because it is selected by something. You do not change your name because you are selected . It is the other way around: The other (who selected you) knows you are selected. So he is referring to you.

I’m not sure what you mean

Yes, you can let the selected object know it is selected, but coding it into a name is not a good choice. Imagine the name already contains a “<”. Beside of that it is unnecessary complicate.

For selection I suggest to have a selection object, that either refers to the selected object

Code:
selectedObject = “Tom”
or it contains enough information for an efficient access.
Code:
selectedAgeIndexs = 0
selectedNameIndex = 1


getSelectedName():
return ExList[selectedAgeIndexs, selectedNameI

I know this is not a good idea, but I can’t find a good one, I’ve done what you have there in the script but how does the user see it. right now it is just a list of names, all text, I can’t do mouse over and click or anything like that. Have any Ideas?

Just have a separate pointer object point to the word instead of it being part of the word.

How does the user see the names right now?

How do you want to present a selected name to a user? (surrounded by a box, placed in a bubble, scaled up, blinking etc.) I do not even know how you present all these names to the user.

Look at your OS:

  • a selected file name gets an highlighted background. You might even see more details of the selected file in a status bar.

Look at Blender:

  • a selected object gets orange wire frame (or orange outline),
  • it is even highlighted in outliner (another representation of the objects)
  • the property editor shows the attributes of the selected object
  • the T and N menus belong to the selected object too.

There are many options to present a selection.