Python 3.x 将结果从集合转换为字符串后,如何在单独的行上打印结果?

Python 3.x 将结果从集合转换为字符串后,如何在单独的行上打印结果?,python-3.x,Python 3.x,我目前正在尝试与文本文件进行比较,看看它们在两个文件中是否有共同之处 文本文件如下所示 ENGLISH.TXT circle table year competition 我目前的代码是让他们打印,我删除了所有不必要的扭曲括号等,但我不能让他们打印在不同的行 List = open("english.txt").readlines() List2 = open("french.txt").readlines() anb = set(List) & set(List2) anb = s

我目前正在尝试与文本文件进行比较,看看它们在两个文件中是否有共同之处

文本文件如下所示

ENGLISH.TXT
circle
table
year
competition
我目前的代码是让他们打印,我删除了所有不必要的扭曲括号等,但我不能让他们打印在不同的行

List = open("english.txt").readlines()
List2 = open("french.txt").readlines()

anb = set(List) & set(List2)
anb = str(anb)

anb = (str(anb)[1:-1])
anb = anb.replace("'","")
anb = anb.replace(",","")
anb = anb.replace('\\n',"")

print(anb)
预计输出将把两个结果分离到新的行中

Currently Happening: 
Competition Table
提前谢谢!
-Xphoon

嗨,我建议你尝试两件事作为一种好的实践: 1) 使用“with”打开文件

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:
##your python operations for the file
2) 如果您使用的是Python 3,请尝试使用“f-String”机会:

print(f"Hello\nWorld!")
这篇文章很好地解释了为什么要使用“with”语句:) 此外,如果要打印出变量,请按照以下方式打印f字符串:

 print(f"{variable[index]}\n variable2[index2]}")
应打印: 你好,世界!分道扬镳

这里有一个解决方案,包括在集合和列表之间转换:

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   anb = set(english_words) & set(french_words)
   anb_list = [item for item in anb]
   for item in anb_list:
       print(item)
with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   for english_word in english_words:
       for french_word in french_words:
           if english_word == french_word:
               print(english_word)
另一个解决方案是将单词保留在列表中:

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   anb = set(english_words) & set(french_words)
   anb_list = [item for item in anb]
   for item in anb_list:
       print(item)
with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   for english_word in english_words:
       for french_word in french_words:
           if english_word == french_word:
               print(english_word)

嗨,我建议你尝试两件事作为一个好的实践: 1) 使用“with”打开文件

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:
##your python operations for the file
2) 如果您使用的是Python 3,请尝试使用“f-String”机会:

print(f"Hello\nWorld!")
这篇文章很好地解释了为什么要使用“with”语句:) 此外,如果要打印出变量,请按照以下方式打印f字符串:

 print(f"{variable[index]}\n variable2[index2]}")
应打印: 你好,世界!分道扬镳

这里有一个解决方案,包括在集合和列表之间转换:

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   anb = set(english_words) & set(french_words)
   anb_list = [item for item in anb]
   for item in anb_list:
       print(item)
with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   for english_word in english_words:
       for french_word in french_words:
           if english_word == french_word:
               print(english_word)
另一个解决方案是将单词保留在列表中:

with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   anb = set(english_words) & set(french_words)
   anb_list = [item for item in anb]
   for item in anb_list:
       print(item)
with open('english.txt', 'r') as englishfile, open('french.txt', 'r') as frenchfile:

   english_words = englishfile.readlines()
   english_words = [word.strip('\n') for word in english_words]
   french_words = frenchfile.readlines()
   french_words = [word.strip('\n') for word in french_words]

   for english_word in english_words:
       for french_word in french_words:
           if english_word == french_word:
               print(english_word)

谢谢你的帮助!一些很棒的提示。没问题,希望你能掌握代码-你知道列表理解吗?工作原理如下<代码>[列表中项目对应的项目应该如何处理]感谢您的帮助!一些很棒的提示。没问题,希望你能掌握代码-你知道列表理解吗?工作原理如下<代码>[列表中项目对应的项目会发生什么情况]