Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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
Python 计算特定字符串在列表中出现的频率_Python_String_List_Numpy - Fatal编程技术网

Python 计算特定字符串在列表中出现的频率

Python 计算特定字符串在列表中出现的频率,python,string,list,numpy,Python,String,List,Numpy,我想用一种“单词袋”的方法成对比较几个列表。我的列表中只有字符串。 不幸的是,我的脚本中有一个无法修复的bug。 如果列表中有数字,代码就可以工作,但一旦列表中有字符串,代码就不再运行。我感谢你的帮助 我收到以下错误消息: Traceback (most recent call last): File "test.py", line 21, in <module> bow_matrix[0, p] = list_words_ab[p] ValueError: could not co

我想用一种“单词袋”的方法成对比较几个列表。我的列表中只有字符串。
不幸的是,我的脚本中有一个无法修复的bug。
如果列表中有数字,代码就可以工作,但一旦列表中有字符串,代码就不再运行。我感谢你的帮助

我收到以下错误消息:

Traceback (most recent call last):
File "test.py", line 21, in <module>
bow_matrix[0, p] = list_words_ab[p]
ValueError: could not convert string to float: 'd'
回溯(最近一次呼叫最后一次):
文件“test.py”,第21行,在
bow_矩阵[0,p]=列表词_ab[p]
ValueError:无法将字符串转换为浮点:“d”
我的代码:

a = ["a", "b", "c", "d"]
b = ["b", "c", "d", "e"]

p = 0
if len(a) > len(b):
    max_words = len(a)
else:
    max_words = len(b)
list_words_ab = list(set(a) | set(b))
len_bow_matrix = len(list_words_ab)
bow_matrix = numpy.zeros(shape = (3, len_bow_matrix))

while p < len_bow_matrix:
    bow_matrix[0, p] = list_words_ab[p]
    p = p+1
p = 0
while p < len_bow_matrix:
    bow_matrix[1, p] = a.count(bow_matrix[0, p])
    bow_matrix[2, p] = b.count(bow_matrix[0, p])
    p = p+1
a=[“a”、“b”、“c”、“d”]
b=[“b”、“c”、“d”、“e”]
p=0
如果len(a)>len(b):
max_words=len(a)
其他:
max_words=len(b)
列表单词=列表(集合(a)|集合(b))
len\u bow\u矩阵=len(列出单词)
bow_矩阵=numpy.zero(形状=(3,len_bow_矩阵))
而p
默认情况下
numpy.zeros
生成一个空浮点数数组,要使用需要指定的字符串
dtype=str

bow_matrix = numpy.zeros(shape = (3, len_bow_matrix),dtype=str)

请提供示例输入和您所需的输出例如a=[NLS,VS40,AMPR],b=[VS40,VS40,AMPR,GFP]我想得到一个如下的矩阵:[[NLS,VS40,AMPR,GFP],[1,1,1,0],[0,2,1,1]]在第一行中,两个列表中的所有字符串都是并集(没有重复项)在第二行/第三行中,我想知道该字符串在列表a/b中出现的频率。我相信使用
numpy.zero
会创建一个期望浮动的矩阵。
dtype=str
只生成
S1
,单字符单词。这在这里很好,但在更一般的情况下不行。