== comparisons against lines from readlines() fail

前端 未结 3 1071
春和景丽
春和景丽 2021-01-28 21:30

I am currently working on a small anagram program that takes all possible permutations of a word and compare them with a dictionary. However, I am unable to get the results to

3条回答
  •  情歌与酒
    2021-01-28 22:32

    compare = txt.readlines()
    

    readlines() doesn't strip the line endings from each line, so each line will have a \n at the end. That causes all your comparisons against compare[j] to fail.

    You could remove the \n's with something like.

    compare = [line.strip() for line in txt]
    

提交回复
热议问题