Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取文本文件中的所有唯一字符?unix/python_Python_Unix_Character_Text Files_Unique - Fatal编程技术网

如何获取文本文件中的所有唯一字符?unix/python

如何获取文本文件中的所有唯一字符?unix/python,python,unix,character,text-files,unique,Python,Unix,Character,Text Files,Unique,有没有其他方法可以获取文本文件中所有唯一字符的列表 我试过下面的方法,但有没有更像蟒蛇的方法?在unix命令中是否可以实现相同的输出 >>> import codecs >>> from collections import Counter >>> with codecs.open('my.txt','r','utf8') as fin: ... x = Counter(fin.read()) ... print [i for

有没有其他方法可以获取文本文件中所有唯一字符的列表

我试过下面的方法,但有没有更像蟒蛇的方法?在unix命令中是否可以实现相同的输出

>>> import codecs
>>> from collections import Counter
>>> with codecs.open('my.txt','r','utf8') as fin:
...     x = Counter(fin.read())
...     print [i for i in x if len(i) == 1]
... 
[u'\u2014', u'\u2018', u'\u201c', u' ', u'\xa3', u'$', u'(', u',', u'0', u'4', u'8', u'@', u'D', u'H', u'L', u'P', u'T', u'\xd7', u'X', u'`', u'd', u'h', u'l', u'p', u't', u'x', u'\ufffd', u'\ufeff', u'\u2013', u'#', u"'", u'+', u'/', u'3', u'7', u';', u'?', u'C', u'G', u'K', u'O', u'S', u'W', u'_', u'\xe0', u'c', u'g', u'k', u'\u2026', u'o', u's', u'w', u'\n', u'"', u'&', u'*', u'\xad', u'.', u'2', u'6', u':', u'>', u'B', u'F', u'J', u'N', u'R', u'V', u'Z', u'b', u'f', u'\xe9', u'j', u'n', u'r', u'v', u'z', u'\t', u'\u2019', u'\u201d', u'!', u'%', u')', u'-', u'1', u'5', u'9', u'=', u'A', u'\xc2', u'E', u'I', u'M', u'Q', u'U', u'Y', u'a', u'\xe2', u'e', u'i', u'm', u'q', u'u', u'y']

一种方法是使用集合:

fh = open('my.txt','r').read()
unique_chars = set(fh)
len(unique_chars) #for the length.

一种方法是使用集合:

fh = open('my.txt','r').read()
unique_chars = set(fh)
len(unique_chars) #for the length.

在我看来,这很像蟒蛇。如果在此之后您不打算使用
x
,那么如果len(i)==1,您可以在计数器中执行
[i for i(fin.read())
,但这对我来说没有意义
len(i)
将始终仅为
1
。你想做什么?如果你不需要计算字符数,你可以调用
set(find.read())
@doukremt看起来他想收集所有只出现一次的字符
set
方法将不起作用。我觉得这很像Python。如果在此之后您不打算使用
x
,如果len(i)==1,您可以在计数器中执行
[i for i(fin.read())
,但这对我来说没有意义
len(i)
将始终仅为
1
。你想做什么?如果你不需要计算字符数,你可以调用
set(find.read())
@doukremt看起来他想收集所有只出现一次的字符<代码>设置方法无效。看起来他想收集所有只出现一次的字符<代码>设置方法不起作用。他的代码就是这么说的。他的问题不同。看起来他想收集所有只出现过一次的人物<代码>设置方法不起作用。他的代码就是这么说的。他的问题不同。