从列表中删除重复的字符串?
removing duplicate strings from the list?
我编写了一个程序来从 'From:' 开始的文本文件中提取所有电子邮件地址。我创建了一个列表来将所有提取的电子邮件地址存储到列表中,并创建了另一个列表来仅存储唯一的电子邮件地址,方法是删除重复的电子邮件地址。现在我得到了输出,但同时我得到了在打印新列表之前显示“设置”的输出,即在 "print Unique_list"
之后
注意 - 未附上原始文本文件,因为我不知道该怎么做。
谢谢
print "Please enter the file path:\n"
text_file = raw_input ("Enter the file name:")
print "Opening File\n"
#Using try and except to print error message if file not found
try:
open_file = open ( text_file )
print "Text file " + text_file + " is opened \n"
except:
#Printing error message
print "File not found"
#Using "raise SystemExit" for program termination if file not found
raise SystemExit
#Creating dynamic list to store the no. Email addresses starting from 'From:'
Email_list = [];
#Using loop to extract the Email addresses starting from 'From:'
for line in open_file:
if 'From:' in line:
#Adding each extracted Email addresses at the end of the list
Email_list.append(line)
print "Printing extracted Email addresses\n"
print Email_list,"\n"
print "Before removing duplicate Email addresses, the total no. of Email addresses starting with 'From:'",len(Email_list),"\n"
#Removing duplicate Email addresses
Unique_list = set(Email_list)
#print Email_list.count()
print "Printing Unique Email addresses\n"
print (Unique_list)
print "After removing duplicate Email addresses, the total no. of Email [enter image description here][1]address starting with From:, ",len(Unique_list),"\n" )`
getting output which shows 'set'before printing the new list i.e after "print Unique_list"
再次将其转换回 list
。
Unique_list = set(Email_list)
Unique_list = list(Unique_list)
#print Email_list.count()
print "Printing Unique Email addresses\n"
print (Unique_list)
答案可能取决于目标。根据问题,目标是否专门以特定方式打印地址尚不清楚;或者根据一些可读性假设来打印它们。如果目标是以给定的所需方式进行打印,则可以通过控制输出来为您提供良好的服务;而不是依赖于您要打印的对象的内置字符串表示形式。
一个例子:
而不是 print Email_list,"\n"
使用 print print (','.join (Email_list, '\n'))
如果您想模拟列表的表示,您可以使用 print ('[\'{list}\']'.format (list = '\', \''.join (Email_list)), '\n')
或者更有凝聚力的东西。
在任何情况下,您都可以控制打印方式。
如果您依赖内部确定的对象表示进行打印,您可能会被迫根据输出问题进行编码考虑;并且这不是支持一个人为纯程序逻辑做出最佳编码选择的能力的选择。
或者,我是不是误解了你的问题?
我编写了一个程序来从 'From:' 开始的文本文件中提取所有电子邮件地址。我创建了一个列表来将所有提取的电子邮件地址存储到列表中,并创建了另一个列表来仅存储唯一的电子邮件地址,方法是删除重复的电子邮件地址。现在我得到了输出,但同时我得到了在打印新列表之前显示“设置”的输出,即在 "print Unique_list"
之后注意 - 未附上原始文本文件,因为我不知道该怎么做。
谢谢
print "Please enter the file path:\n"
text_file = raw_input ("Enter the file name:")
print "Opening File\n"
#Using try and except to print error message if file not found
try:
open_file = open ( text_file )
print "Text file " + text_file + " is opened \n"
except:
#Printing error message
print "File not found"
#Using "raise SystemExit" for program termination if file not found
raise SystemExit
#Creating dynamic list to store the no. Email addresses starting from 'From:'
Email_list = [];
#Using loop to extract the Email addresses starting from 'From:'
for line in open_file:
if 'From:' in line:
#Adding each extracted Email addresses at the end of the list
Email_list.append(line)
print "Printing extracted Email addresses\n"
print Email_list,"\n"
print "Before removing duplicate Email addresses, the total no. of Email addresses starting with 'From:'",len(Email_list),"\n"
#Removing duplicate Email addresses
Unique_list = set(Email_list)
#print Email_list.count()
print "Printing Unique Email addresses\n"
print (Unique_list)
print "After removing duplicate Email addresses, the total no. of Email [enter image description here][1]address starting with From:, ",len(Unique_list),"\n" )`
getting output which shows 'set'before printing the new list i.e after "print Unique_list"
再次将其转换回 list
。
Unique_list = set(Email_list)
Unique_list = list(Unique_list)
#print Email_list.count()
print "Printing Unique Email addresses\n"
print (Unique_list)
答案可能取决于目标。根据问题,目标是否专门以特定方式打印地址尚不清楚;或者根据一些可读性假设来打印它们。如果目标是以给定的所需方式进行打印,则可以通过控制输出来为您提供良好的服务;而不是依赖于您要打印的对象的内置字符串表示形式。
一个例子:
而不是 print Email_list,"\n"
使用 print print (','.join (Email_list, '\n'))
如果您想模拟列表的表示,您可以使用 print ('[\'{list}\']'.format (list = '\', \''.join (Email_list)), '\n')
或者更有凝聚力的东西。
在任何情况下,您都可以控制打印方式。
如果您依赖内部确定的对象表示进行打印,您可能会被迫根据输出问题进行编码考虑;并且这不是支持一个人为纯程序逻辑做出最佳编码选择的能力的选择。
或者,我是不是误解了你的问题?