I have two lists that are generated automatically in Python (using String and Random). Say I ran the code and currently have them like this:
random_word = ['Sirius','July','Seven']
random_char = ['#','$','%']
The desired output is something like this:
random_list = ['Siriu$s', 'J%uly', 'S#even']
Note 1: The special characters can be positioned either all at the same position or at any random position
Now, this is the part where I've stopped, because I haven't found a way yet to -at least- put the special characters at any specific position in each string of the list random_word
:
for k in random_words:
for i,j in list(enumerate(random_char)):
random_list = k.insert(i*2, j)
print(random_list)
Note 2: I know my mistake above, k is a str and can't use the insert()
(that's the main error I know for now :P). However, I wanted to show my thought of the process of what I initially was attempting to do; putting a random character j
from random_char
at the second position i*2
of the random word without replacing it, using two for loops
As always any guidance on how to make this right is very well appreciated! :)
CodePudding user response:
IIUC, one way using random.randrange
with random.shuffle
:
random_word = ['Sirius','July','Seven']
random_char = ['#','$','%']
random.shuffle(random_char)
for w, c in zip(random_word, random_char):
i = random.randrange(0, len(w) 1)
print(w[:i] c w[i:])
Possible output (since it's random):
S#irius
%July
S$even