fork download
  1. # https://stackoverflow.com/questions/49512206/loosing-element-value-when-looping-python-list
  2.  
  3. from difflib import SequenceMatcher
  4.  
  5. def similar(a, b):
  6. s = SequenceMatcher(a, b).ratio()
  7. s = round(s * 100, 1)
  8. return s
  9.  
  10.  
  11. list1=['aaaa','cccc','bb']
  12. list2=['aaa','fff','v']
  13.  
  14. for word1 in list1:
  15. for word2 in list2:
  16. result = similar(word1, word2)
  17. print('Comparing "{}" and "{}", got {}'.format(
  18. word1, word2, result))
  19. if similar(word1, word2) > 0:
  20. print(word2)
  21.  
  22. print('Loop finished, contents of lists:')
  23. print('list1: {}'.format(repr(list1)))
  24. print('list2: {}'.format(repr(list2)))
Success #stdin #stdout 0.04s 64476KB
stdin
Standard input is empty
stdout
Comparing "aaaa" and "aaa", got 0.0
Comparing "aaaa" and "fff", got 0.0
Comparing "aaaa" and "v", got 0.0
Comparing "cccc" and "aaa", got 0.0
Comparing "cccc" and "fff", got 0.0
Comparing "cccc" and "v", got 0.0
Comparing "bb" and "aaa", got 0.0
Comparing "bb" and "fff", got 0.0
Comparing "bb" and "v", got 0.0
Loop finished, contents of lists:
list1: ['aaaa', 'cccc', 'bb']
list2: ['aaa', 'fff', 'v']