-2

Am trying to check the existence of a list of strings in other list, it might exist more that one time, so when I print the result am getting more then 1 occurance in the result and I want it to print each occurance one time:

This is what I have been doing so far:

    for i in range (0, len(spec)):
        if spec[i] in my_list:
            print ("R7:You mean:",spec[i])

spec is a list of strings that am trying to test if each one of them exists in other list (my_list), strings might be duplicated in my_list ( a string could exist more than one time)

The output am getting :

R7:You mean: manège
R7:You mean: mangée
R7:You mean: manège
R7:You mean: mangée
R7:You mean: manège
R7:You mean: mangée
R7:You mean: manège
R7:You mean: mangée
R7:You mean: manège
R7:You mean: mangée
R7:You mean: manège
R7:You mean: mangée

The output am expecting:

R7:You mean: manège
R7:You mean: mangée

Am missing something I don't know what it is !

Ran
  • 635
  • 2
  • 10
  • 22
  • 2
    what are `L` and `result`? – noamgot Dec 31 '17 at 13:25
  • Just use `break` in you for loop and it will stop at the first occurrence :) – Cajuu' Dec 31 '17 at 13:26
  • 1
    When you say "it might exist more that one time", do you mean strings can be duplicated in `spec`, or in `my_list`? – John Gordon Dec 31 '17 at 13:27
  • each time am trying to replace a character with a special character in L (which is list) to see if it exist in my_list, thought strings can be duplicated in my_list @johnGordon – Ran Dec 31 '17 at 13:29
  • @noamgot L is a list with special character, result is the input – Ran Dec 31 '17 at 13:31
  • @cajuu' That will prevent 'mangée', it will only print the first one, Thant's not what I need :/ – Ran Dec 31 '17 at 13:35
  • Right now it looks like `spec` has 2 string items which has 5 duplicates each. – Nae Dec 31 '17 at 13:40
  • I edited the question, I hope you can undrestand me now – Ran Dec 31 '17 at 13:42
  • I would also put the return of `print(spec)` and `print(my_list)` unless they are insanely long. – Nae Dec 31 '17 at 13:43
  • my_list is a dictionary, so it contains an unfinite number of strings, spec is a list of strings after applying certain modification to an input (The main idea is to apply some modification to string and check if it the generated one are actually words that exists in the dictionary) – Ran Dec 31 '17 at 13:46
  • Is that code snippet in another loop? – Nae Dec 31 '17 at 13:49
  • Yes, it is snipped – Ran Dec 31 '17 at 13:53

2 Answers2

2

You can use set comprehension instead of a list comprehension:

spec_unique = {result[:i]+s+result[i+1:]for i in range(len(result)) for s in L if s!=result[i]}
for s in spec_unique:
    if s in my_list:
        print ("R7:You mean:", s)
Noa
  • 145
  • 1
  • 9
0

12 lines of print statement indicate that print statement is run 12 times given that this is the only print statement being fed to stdout, and the statement isn't nested in an additional loop.

Which means len(spec) >= 12. I think spec has 5 duplicates of 2 unique elements in it.

Try:

uniq_spec = set(spec)
for item in uniq_spec:
    if item in my_list:
        print("R7:You mean:", item)

see example:

if __name__ == '__main__':
    str_list = ["words", "many", "many", "words"]
    word_list = ["mana", "many", "word", "words", "words", "again"]

    for item in set(str_list):
        if item in word_list:
            print(item)

    input()
Nae
  • 14,209
  • 7
  • 52
  • 79
  • there are actually more than 12 lines of `print`, and I tried that actually but no luck, what I thinks is that duplications exist in my_list – Ran Dec 31 '17 at 14:08
  • @Ran that wouldn't result in the same output. See new example there are duplicates in both of my lists yet the result is different. It may be that your _for loop_ is run more than 6 times. – Nae Dec 31 '17 at 14:11
  • 1
    It was a problem of loop as you said, I didn't pay attention for it, my bad :/ – Ran Dec 31 '17 at 14:59