my_list = ['cat','cow','dog','rabbit']
but what I want is to append/insert a character(or string) to one or more element(not all).
something like
my_list = ['cat','cow_%s','dog_%s','rabbit']%('gives milk','bark')
now updated list should look like
my_list = ['cat','cow_gives milk','dog_bark','rabbit']
one way is to do this is manually change/update the element one by one eg my_list[2]=my_list[2]+"bark"
but I don't want that because my_list is long(around 100s element ) and 40+ need to be changed dynamically.
In my case It is like
my_list = ['cat','cow_%s','dog_%s','rabbit']
for a in xyz: #a is a string and xyz is a list of string
my_list=my_list%(a,a+'b')
fun(my_list)
You could do something like this:
changes = {"cow":"gives milk", "dog":"bark"}
my_list = [item if item not in changes else "_".join([item, changes[item]])
for item in my_list]
For greater efficiency if you will do this repeatedly, as JAB suggests in the comments, build a dictionary to map items to their locations (indices) in the list:
locations = dict((s, i) for i, s in enumerate(my_list))
You can then use this to find the corresponding items.
If you are stuck with the list of strings, some ending "%s"
, and list of things to put in them, I guess you will have to do something like:
for i, s in enumerate(my_list): # work through items in list with index
if s.endswith("%s"): # find an item to update
my_list[i] = s % xyz.pop(0) # update with first item popped from xyz
Note that xyz
will have to be a list for this, not tuple, as you can't pop
items from a tuple.
If you have a replacement string in source strings, and an iterable of replacements, then you can do something such as:
import re
def do_replacements(src, rep):
reps = iter(rep)
for item in src:
yield re.sub('%s', lambda m: next(reps), item)
replaces = ('gives milk','bark', 'goes walkies', 'eat stuff')
my_list = ['cat','cow_%s','dog_%s_and_%s','rabbit_%s']
print list(do_replacements(my_list, replaces))
# ['cat', 'cow_gives milk', 'dog_bark_and_goes walkies', 'rabbit_eat stuff']
If you don't have enough replacements you'll get a StopIteration
- you can either consider than an error, or alternatively provide a default (possibly empty) replacement to the replacement: lambda m: next(reps, '')
for instance...
Here are two more possibilities: Either, you could create a list of verbs alongside your list of animals, with None
for animals that shall not have a verb, and merge them together...
animals = ['cat','cow','dog','rabbit']
verbs = [None, 'gives milk', 'barks', None]
def combine(animal, verb):
return (animal + "_" + verb) if verb else animal
print map(combine, animals, verbs)
... or, if the animals already have those %s
placeholders for verbs, iterate the animals, check if the current animal has a placeholder, and if so, replace it with the next verb. (Similar to Jon's answer, but using %
instead of re.sub
)
animals = ['cat','cow_%s','dog_%s','rabbit']
verbs = iter(['gives milk', 'barks'])
print [animal % next(verbs) if '%' in animal else animal
for animal in animals]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.