【发布时间】:2013-11-19 02:27:25
【问题描述】:
我是 Python 新手,我发现 set() 有点令人困惑。有人可以提供一些帮助来查找和创建新的唯一数字列表(换句话说,消除重复)?
import string
import re
def go():
import re
file = open("C:/Cryptography/Pollard/Pollard/newfile.txt","w")
filename = "C:/Cryptography/Pollard/Pollard/primeFactors.txt"
with open(filename, 'r') as f:
lines = f.read()
found = re.findall(r'[\d]+[^\d.\d+()+\s]+[^\s]+[\d+\w+\d]+[\d+\^+\d]+[\d+\w+\d]+', lines)
a = found
for i in range(5):
a[i] = str(found[i])
print(a[i].split('x'))
现在
print(a[i].split('x'))
....给出以下输出
['2', '3', '1451', '40591', '258983', '11409589', '8337580729',
'1932261797039146667']
['2897', '514081', '585530047', '108785617538783538760452408483163']
['2', '3', '5', '19', '28087', '4947999059',
'2182718359336613102811898933144207']
['3', '5', '53', '293', '31159', '201911', '7511070764480753',
'22798192180727861167']
['2', '164493637239099960712719840940483950285726027116731']
如何输出仅包含非重复数字的列表?我在论坛上读到“set()”可以做到这一点,但我试过这个没有用。非常感谢任何帮助!
【问题讨论】:
-
我不确定我是否理解。您显示的所有列表都没有内部重复值。您是否担心其他一些值中存在重复项,但是(巧合)您显示的前五个值中没有重复项?或者您是否需要消除列表之间的重复项,使
2只出现在第一个列表中,而不出现在第三个或第五个列表中? -
很抱歉,它已经深夜了,我的意思是“如果我将所有列表连接在一起,则没有重复值”
标签: python list python-3.x unique