Python 如何将集合转换为浮点或整数

Python 如何将集合转换为浮点或整数,python,class,python-3.x,set,nltk,Python,Class,Python 3.x,Set,Nltk,我感兴趣的是绘制整数向量u中的唯一值与这些唯一值在u中出现的次数(即,在u中出现的唯一值的频率分布)之间的关系 正如您在最后几行代码中所看到的,为了得到u中唯一值的新向量y,我运行“y=set(u)”,并指定“x=FreqDist(u)”,到目前为止还不错。当我尝试使用matplotlib的“散布”来绘制x和y时,问题出现了。我得到“TypeError:float()参数必须是字符串或数字,而不是“set” 完整回溯: Traceback (most recent call last): Fil

我感兴趣的是绘制整数向量u中的唯一值与这些唯一值在u中出现的次数(即,在u中出现的唯一值的频率分布)之间的关系

正如您在最后几行代码中所看到的,为了得到u中唯一值的新向量y,我运行“y=set(u)”,并指定“x=FreqDist(u)”,到目前为止还不错。当我尝试使用matplotlib的“散布”来绘制x和y时,问题出现了。我得到“TypeError:float()参数必须是字符串或数字,而不是“set”

完整回溯:

Traceback (most recent call last):
File "C:/Python34/first_program.py", line 45, in <module>
plt.scatter(x,y)
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3200, in scatter
linewidths=linewidths, verts=verts, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 3674, in scatter
self.add_collection(collection)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1477, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "C:\Python34\lib\site-packages\matplotlib\collections.py", line 192, in get_datalim
offsets = np.asanyarray(offsets, np.float_)
File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 525, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
TypeError: float() argument must be a string or a number, not 'set'
回溯(最近一次呼叫最后一次):
文件“C:/Python34/first_program.py”,第45行,在
plt.散射(x,y)
文件“C:\Python34\lib\site packages\matplotlib\pyplot.py”,第3200行,分散显示
线宽=线宽,顶点=顶点,**kwargs)
文件“C:\Python34\lib\site packages\matplotlib\axes\\u axes.py”,第3674行,分散显示
self.add_集合(集合)
文件“C:\Python34\lib\site packages\matplotlib\axes\\u base.py”,第1477行,在add\u集合中
self.update_datalim(collection.get_datalim(self.transData))
get\U datalim中的文件“C:\Python34\lib\site packages\matplotlib\collections.py”,第192行
偏移量=np.asanyarray(偏移量,np.float)
asanyarray中的文件“C:\Python34\lib\site packages\numpy\core\numeric.py”,第525行
返回数组(a,dtype,copy=False,order=order,subok=True)
TypeError:float()参数必须是字符串或数字,而不是“set”
尝试将y转换为整数或浮点(y=int(y),y=float(y))时会遇到如下错误:

Traceback (most recent call last):
File "C:/Python34/first_program.py", line 44, in <module>
y=int(y)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'
回溯(最近一次呼叫最后一次):
文件“C:/Python34/first_program.py”,第44行,在
y=int(y)
TypeError:int()参数必须是字符串、类似于对象的字节或数字,而不是“set”

仅供参考-我正在Windows 7 64位计算机上使用32位python v3.4.3。(64位python v3.5中出现了一些nltk错误,因此必须使用早期版本。)

您可以使用pandas.DataFrame轻松做到这一点:

import pandas as pds
df = pds.DataFrame(data=[txtwords],columns=['word'])
df.reset_index(inplace=True)  #just to have a column to count
df.groupby('word').count().plot()
[集合中x的浮动(x)]
import pandas as pds
df = pds.DataFrame(data=[txtwords],columns=['word'])
df.reset_index(inplace=True)  #just to have a column to count
df.groupby('word').count().plot()