Home > OS >  Manipulate string in for loop
Manipulate string in for loop

Time:06-03

I'm quite new to Python regarding string manipulation. I've a field containing some object labels and want to insert text before or behind it. For this I'm using values from a list to search the string for specific values. Once I do this I'm not able to manipulate all found results, only for just one value. I know that strings work differently in Python (immutable), but don't know how to resolve this problem any other way.

objectList = [('Object A','Object B','Object C', 'Object D')]
objectString = '"Object A", "Object B", "Object C", "Object D"'

def transform_toString(objects, old_format):
    obj = objects
    new_format = old_format
    for o in obj:
        position = new_format.find(str(o))
        new_format = new_format[:position]   ' found'   new_format[position:]
    return new_format
    
transformed_string = transform_toString(objectList, objectString)

This results in the following output:

"Object A", "Object B", "Object C", "Object D found"

How can I achieve the following output?

"Object A found", "Object B found", "Object C found", "Object D found"

CodePudding user response:

There are multiple errors in your code.

  1. I would recommend you to install an IDE with debugger, so that you can jump in the forloop and see what is happening there.

  2. If you look a bit closer, you can see that you are iterating an array of 1 element and that is ('Object A','Object B','Object C', 'Object D') (it is a tuple of 4 items) and you are searching for the whole tuple in the string. This would normally result in an error in different programming languages, however here it tries to find the whole tuple

  3. And the reason why it writes the found at the end is that if you look at the return value (I recommend checking out documentation as well) iit returns the -1 which means the element was not found. In python, -1 indicates the last element in the array (in this case last character in the string)

However, if you fix the type, the result still not be the desired one. If you remove the parantases, you will get " foundObject A", " foundObject B", " foundObject C", " foundObject D". In order to fix this, you need to move your pointer (position) by the offset of the word length.

The fixed code:

objectList = ['Object A', 'Object B', 'Object C', 'Object D']
objectString = '"Object A", "Object B", "Object C", "Object D"'


def transform_toString(objects, old_format):
    obj = objects
    new_format = old_format
    for o in obj:
        position = new_format.find(str(o))
        if position == -1:
            continue
        position  = len(o)
        new_format = new_format[:position]   ' found'   new_format[position:]
    return new_format


transformed_string = transform_toString(objectList, objectString)
print(transformed_string)
#  "Object A found", "Object B found", "Object C found", "Object D found"

Additionally, I recommend you to first try to debug your code and then ask a question. This seems like a simple task given to you at school.

CodePudding user response:

You're pretty close the thing is that you put your tuple inside of a list so iterating over objectList won't work. You'd have to iterate over objectList[0] as this list only contains one element.

Also you have to adjust the position by the length of your string.

objectList = [('Object A','Object B','Object C', 'Object D')]
objectString = '"Object A", "Object B", "Object C", "Object D"'

def transform_toString(objects, old_format):
    obj = objects # you dont need that
    new_format = old_format # you dont need that
    for o in obj[0]:
        position = new_format.find(str(o))
        if position == -1:
            continue
        objlen = len(o)
        position = new_format.find(str(o))   objlen
        new_format = new_format[:position]   ' found'   new_format[position:]
    return new_format
    
transformed_string = transform_toString(objectList, objectString)
  • Related