[英]Python- making a set from a text file
我有一个文本文件。 它的胆量看起来像这样/全部看起来像这样(已被编辑。这也不是最初看起来的样子)
(0, 16, 0)
(0, 17, 0)
(0, 18, 0)
(0, 19, 0)
(0, 20, 0)
(0, 21, 0)
(0, 22, 0)
(0, 22, 1)
(0, 22, 2)
(0, 23, 0)
(0, 23, 4)
(0, 24, 0)
(0, 25, 0)
(0, 25, 1)
(0, 26, 0)
(0, 26, 3)
(0, 26, 4)
(0, 26, 5)
(0, 26, 9)
(0, 27, 0)
(0, 27, 1)
无论如何,如何将这些值放入python 2的集合中?
我最近的尝试是
om_set = set(open('Rye Grass.txt').read()
编辑:这是我用来获取文本文件的代码。 导入cv2导入numpy作为np导入时间
om=cv2.imread('spectrum1.png')
om=om.reshape(1,-1,3)
om_list=om.tolist()
om_tuple={tuple(item) for item in om_list[0]}
om_set=set(om_tuple)
im=cv2.imread('1.jpg')
im=cv2.resize(im,(100,100))
im= im.reshape(1,-1,3)
im_list=im.tolist()
im_tuple={tuple(item) for item in im_list[0]}
ColourCount= om_set & set(im_tuple)
with open('Weedlist', 'a') as outputfile:
output = ', '.join([str(tup) for tup in sorted(ColourCount)])
outputfile.write(output)
print 'done'
im=cv2.imread('2.jpg')
im=cv2.resize(im,(100,100))
im= im.reshape(1,-1,3)
im_list=im.tolist()
im_tuple={tuple(item) for item in im_list[0]}
ColourCount= om_set & set(im_tuple)
with open('Weedlist', 'a') as outputfile:
output = ', '.join([str(tup) for tup in sorted(ColourCount)])
outputfile.write(output)
print 'done'
正如@TimPietzcker所建议并信任的文件那样,它们只有用逗号分隔的三元组的整数的固定表示形式,并用括号括起来,是一个简单的解析器(OP的问题也有贪婪的“读取”文件成备忘录):
#! /usr/bin/env python
from __future__ import print_function
infile = 'pixel_int_tuple_reps.txt'
split_pits = None
with open(infile, 'rt') as f_i:
split_pits = [z.strip(' ()') for z in f_i.read().strip().split('),')]
if split_pits:
on_set = set(tuple(int(z.strip())
for z in tup.split(', ')) for tup in split_pits)
print(on_set)
有轨电车
(0, 19, 0), (0, 20, 0), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 27, 0), (0, 29, 2), (0, 35, 2), (0, 36, 1)
变成:
set([(0, 27, 0), (0, 36, 1), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 19, 0), (0, 35, 2), (0, 29, 2), (0, 20, 0)])
小片段:
分割像素整数三胞胎成的子串0, 19, 0
清洁位的杂散括号和空格远离(也末照顾右括号的。
如果“可行”,则进一步将带有整数转换元组的rgb拆分输入一组。
在对这种反序列化任务使用eval / exec之前,我会三思而后行。
更新由OP(请更新的问题!)意见的建议:
...所以直到我们从OP获得更多信息为止:
对于理论上干净的3位元组转储文件,此答案有效(如果不是太大而无法一次加载并映射到集合中)。
对于具体任务,如果已经将足够多的新信息添加到问题中,我可以更新答案;-)
一种方法,如果三元“行”与上一阶段连接在一起,有或没有换行符分隔,但始终缺少逗号,则可以更改文件读取部分:
s = s | fresh
解决“隔离”问题的s = s | fresh
事物 或如果像这样添加这些“块” (0, 1, 230)(13, ...
即)(
“努力击中”:
f_i.read().strip().split('),')
编写f_i.read().replace(')('), (', ').strip().split('),')
...就是将)(
部分)(
“固定”到), (
部分,使其能够继续下去,就好像它是同质的“结构”一样。 现在更新以解析数据集的版本2(更新的问题):
文件pixel_int_tuple_reps_v2.txt
现在具有:
(0, 16, 0)
(0, 17, 0)
(0, 18, 0)
(0, 19, 0)
(0, 20, 0)
(0, 21, 0)
(0, 22, 0)
(0, 22, 1)
(0, 22, 2)
(0, 23, 0)
(0, 23, 4)
(0, 24, 0)
(0, 25, 0)
(0, 25, 1)
(0, 26, 0)
(0, 26, 3)
(0, 26, 4)
(0, 26, 5)
(0, 26, 9)
(0, 27, 0)
(0, 27, 1)
编码:
#! /usr/bin/env python
from __future__ import print_function
infile = 'pixel_int_tuple_reps_v2.txt'
on_set = set()
with open(infile, 'rt') as f_i:
for line in f_i.readlines():
rgb_line = line.strip().lstrip('(').rstrip(')')
try:
rgb = set([tuple(int(z.strip()) for z in rgb_line.split(', '))])
on_set = on_set.union(rgb)
except:
print("Ignored:" + rgb_line)
pass
print(len(on_set))
for rgb in sorted(on_set):
print(rgb)
现在解析此文件,并首先转储集合和的长度(以及按排序顺序排列的元素):
21
(0, 16, 0)
(0, 17, 0)
(0, 18, 0)
(0, 19, 0)
(0, 20, 0)
(0, 21, 0)
(0, 22, 0)
(0, 22, 1)
(0, 22, 2)
(0, 23, 0)
(0, 23, 4)
(0, 24, 0)
(0, 25, 0)
(0, 25, 1)
(0, 26, 0)
(0, 26, 3)
(0, 26, 4)
(0, 26, 5)
(0, 26, 9)
(0, 27, 0)
(0, 27, 1)
HTH。 请注意,提供的样本输入中没有重复项。 将最后一条数据线加倍,我仍然可以接收到21个唯一元素作为输出,所以我想现在它可以按设计工作;-)
只需要进行少量修改即可。
om_set = set(eval(open('abc.txt').read()))
结果
{(0, 19, 0),
(0, 20, 0),
(0, 21, 1),
(0, 22, 0),
(0, 24, 3),
(0, 27, 0),
(0, 29, 2),
(0, 35, 2)}
编辑这是IPython
提示符中的代码工作。
In [1]: file_ = open('abc.txt')
In [2]: text_read = file_.read()
In [3]: print eval(text_read)
((0, 19, 0), (0, 20, 0), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 27, 0), (0, 29, 2), (0, 35, 2), (0, 36, 1))
In [4]: type(eval(text_read))
Out[1]: tuple
In [5]: print set(eval(text_read))
set([(0, 27, 0), (0, 36, 1), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 19, 0), (0, 35, 2), (0, 29, 2), (0, 20, 0)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.