Python Forum

Full Version: How to iterate over some characters in a string and the others will stay as it is. ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
one = ["one","o","n","e"]
two = ["two","t","w","o"]
t = "one and two"
dict = dict(one = one, two = two)
words = ["one","and","two"]

for i in range(0,len(dict)):
    for j in range(0,len(words)):
        #print(list(dict)[i])
        if words[j] == (list(dict)[i]):
            print(words[j])
            for i in range(0,len(words[j])):
                #print(i)
            #print((dict)[words[j]])
                for k in range(0,len((dict)[words[j]])):
                    z = t.replace(words[j],(dict)[words[j]][k])
                    print(z)

What Output Do I'm getting ?

one and two
o and two
n and two
e and two
one and two
o and two
n and two
e and two
one and two
o and two
n and two
e and two


How about do I want ?

one and two
one and t
one and w
one and o
o and two
o and t
o and w
o and o
n and two
n and t
n and w
n and o
e and two
e and t
e and w
e and o

Please help me out with it, I'm badly stucked on it. Thanks
Isin’t it overly complicated approach to achieve desired result?

Simple would be something along those lines “for every item in first, for every element in second print item and element”
You should have a look at the module itertools
It has a lot of very useful functions included
from itertools import product

one = ["one","o","n","e"]
two = ["two","t","w","o"]

for first, second in product(one, two):
    print(f'{first} and {second}')
Hi,

@sodmzs: iterating with range(len(..)) and the index over an iterable is an anti-pattern, don't do it. Python support direct iteration:

>>> one = ['one', 'o', 'n' ,'e']
>>> two = ['two', 't', 'w', 'o']
>>> for item in one:
...     for other_item in two:
...         print('{} {}'.format(item, other_item))
...
one two
one t
one w
one o
o two
o t
o w
o o
n two
n t
n w
n o
e two
e t
e w
e o
>>>
But using itertools as shown is the more elegant way anyway.

Regards, noisefloor
(Jun-17-2019, 10:16 AM)noisefloor Wrote: [ -> ]Hi,

@sodmzs: iterating with range(len(..)) and the index over an iterable is an anti-pattern, don't do it. Python support direct iteration:

>>> one = ['one', 'o', 'n' ,'e']
>>> two = ['two', 't', 'w', 'o']
>>> for item in one:
...     for other_item in two:
...         print('{} {}'.format(item, other_item))
...
one two
one t
one w
one o
o two
o t
o w
o o
n two
n t
n w
n o
e two
e t
e w
e o
>>>
But using itertools as shown is the more elegant way anyway.

Regards, noisefloor


Thanks for your reply, but I was looking forward for the other words in the string will stay as it is same. That is in my case, the string was "one and two" Means just the "one" And "two" Will iterate and "and" Word will be in the same position. Since, I want to make it flexible so that in case I would have a string like "two and one and two" Then it can work well with this as well.
In your initial post you wrote:
Quote:How about do I want ?

one and two
one and t
one and w
one and o
o and two
o and t
o and w
o and o
n and two
n and t
n and w
n and o
e and two
e and t
e and w
e and o
and you got solutions how to get what you wrote.
Now you say:
Quote:...I was looking forward for the other words in the string will stay as it is same. That is in my case, the string was "one and two" Means just the "one" And "two" Will iterate and "and" Word will be in the same position. Since, I want to make it flexible so that in case I would have a string like "two and one and two" Then it can work well with this as well.
That makes no sense, sorry.
(Jun-17-2019, 02:15 PM)ThomasL Wrote: [ -> ]In your initial post you wrote:
Quote:How about do I want ?

one and two
one and t
one and w
one and o
o and two
o and t
o and w
o and o
n and two
n and t
n and w
n and o
e and two
e and t
e and w
e and o
and you got solutions how to get what you wrote.
Now you say:
Quote:...I was looking forward for the other words in the string will stay as it is same. That is in my case, the string was "one and two" Means just the "one" And "two" Will iterate and "and" Word will be in the same position. Since, I want to make it flexible so that in case I would have a string like "two and one and two" Then it can work well with this as well.
That makes no sense, sorry.

Sorry that u dont feel that it makes sense... But the requirement is the same. I would try to explain it again.

Let's say I have 5 words in a string : the climate is super awesome. Now in this statement let's say I just want to iterate "climate", " Super" And "awesome" Keeping "the" & "is" Static and at the same position. Then how would I write the code for this req
Hi,

Quote:Then how would I write the code for this req
Np big difference: Split the string to a list of strings, extract the strings you want to "shuffle", compute all combinations using the appropriate method from the itertools module, reassemble the strings and insert the static words while doing that.

Now it's up to you to cast that into code :-) In case you need help -> show the code you tried and you will get help.

Regards, noisefloor
In either having a look at what noisefloor wrote or having a look into the itertools modul that has a lot of other useful functions like combinations() or permutations() that you can use for your task. Good luck.
(Jun-17-2019, 03:19 PM)sodmzs Wrote: [ -> ]I would try to explain it again.

Let's say I have 5 words in a string : the climate is super awesome. Now in this statement let's say I just want to iterate "climate", " Super" And "awesome" Keeping "the" & "is" Static and at the same position. Then how would I write the code for this req

This is not 'req', this is 'guessing game'. There is lot of ambiguity in this. Does it mean that there are always 5 word strings and 'static' words are always #2, #4 and #5? If not by which criteria it is determined which words are 'static' and which are not?

From Zen of Python: "In the face of ambiguity, refuse the temptation to guess."