Reputation: 325
Say I have a list:
mylist = [element1, element2, element3, ...... ,elementn]
And the elements are like:
element1 = 'object1 = "value1" object2 = "value2" object3 = "value3"'
and all the other elements are the same, except for a minor difference in "value3"
Replace each of the elements in the list with the likes of element1 and you'll get how the list looks like.
I have a string:
to_place = 'something_to_append'
What I want is to append to_place
at the end of object1 = "value1"
, so that it looks like object1 = "value1something_to_append"
I've tried looping through each element, trying to find value1
(which is the same accross the elements of the list), and using string append functions, but I'm just not getting the results I want.
Any help will be appreciated. (I'll upload the code I've tried in a few minutes, having a bit of trouble)
Edit: Adding the code:
element = [element1, element2, element3]
s = 'something_to_append'
for u in element:
if 'object1="value"' in u.split():
u = u + s
print(element)
As for the output, I'm getting the same list as before. P.S: Using Python 2.7, on PyCharm, on Windows 10
Upvotes: 0
Views: 2153
Reputation: 898
First of all, strings are immutable in python. So you can never change a string in place - you can only copy it, do something with that copy and create a new string from that. In your case, you could define a function to do that for you:
def insert_something(element, value, appendix):
index_after = element.find(value) + len(value)
return element[:index_after] + appendix + element[index_after:]
afterwards, iterate over your list, doing this with every element, and creating a new list (i'm sure that there are more elegant ways than this, but this is what i came up with rn):
new_list = [insert_something(item, '"value1', to_place) for item in mylist]
Edit:
To stay closer to your own code: what you have to do is use enumerate
, since you are going to change the list, which will affect the position of your elements. Could look something like this:
element = [element1, element2, element3]
s = 'something_to_append'
for idx, u in enumerate(element):
if 'object1="value"' in u.split():
u = u[:-1] + s + '"'
element[idx] = u
Both of these work and return what you want, so it's just up to you what you like better :) though the first one is a more general approach for any similar problem.
Upvotes: 1
Reputation: 241
Maybe a simpler answer if all "value1" are the same :
mylist = [element1, element2, element3, ...... ,elementn]
to_place = "something_to_append"
newlist = [elem.replace("value1","value1"+to_place) for elem in mylist]
Upvotes: 0
Reputation: 142126
You might be able to generalise this a bit by splitting the string using shlex.split
so to preserve any whitespace in the value parts, assigning the values to a dict, updating those values, then reconstructing the string, eg:
import shlex
from collections import OrderedDict
def f(text, **kwargs):
split = shlex.split(text)
d = OrderedDict(zip(split[::3], split[2::3]))
d.update((k, d[k] + v) for k, v in kwargs.items() if k in d)
return ' '.join('{} = "{}"'.format(k, v) for k, v in d.items())
Then use it as:
element1 = 'object1 = "value1" object2 = "value2" object3 = "value3"'
updated_string = f(element1, object1='append here')
# 'object1 = "value1append here" object2 = "value2" object3 = "value3"'
Might want to consider introducing a few more checks for robustness but should work equally as well as other answers here.
Upvotes: 1
Reputation: 3892
Substituting with a regex may work for you. Here, I put it in a function to make it easier to use:
import re
def append_to(in_value, value_to_search, value_to_append):
value_to_search = re.escape(value_to_search)
return re.sub(
'"({})"'.format(value_to_search),
'"\\1{}"'.format(value_to_append),
in_value,
)
my_list = [append_to(item, 'value3', 'something_to_append') for item in my_list]
Upvotes: 0
Reputation: 3504
Define a function f
def f(x, y):
return x[:17] + y + x[17:]
Then do
>>> from pprint import pprint
>>> pprint(mylist)
['object1 = "value1" object2 = "value2" object3 = "value3"',
'object1 = "value1" object2 = "value2" object3 = "value3"',
'object1 = "value1" object2 = "value2" object3 = "value3"']
>>> to_place = 'something_to_append'
>>> new_list = [f(x, to_place) for x in mylist]
>>> pprint(new_list)
['object1 = "value1something_to_append" object2 = "value2" object3 = "value3"',
'object1 = "value1something_to_append" object2 = "value2" object3 = "value3"',
'object1 = "value1something_to_append" object2 = "value2" object3 = "value3"']
Upvotes: -1