python 中的算法 Misra 和 Gries 实现
Algorithm Misra & Gries implementation in python
我有这个伪代码:
A = empty associative array;
// Processing
while ( not end of sequence ) #referente ao data stream
j = current_token();
if ( j in keys(A) ) then A[ j ] = A[ j ] + 1;
else if ( | keys(A) | < ( k – 1 ) ) then A[ j ] = 1;
else for each i in keys(A) do
A[ i ] = A[ i ] – 1;
if ( A[ i ] == 0 ) then remove i from A;
// Output
if( a in keys(A) ) then freq_estimate = A[ a ];
else freq_estimate = 0;
我是这样实现的:
def contMisraGries(dataClean, k):
frequencia_MisraGries = {}
A={}
for caracter in dataClean:
if caracter in A.keys():
A = increment(caracter, A)
else:
if A.keys() < k-1:
A.keys(caracter) = 1
else:
for i , v in A.items():
A.keys(i) -= 1
if v == 0:
del A.items(i)
for car, val in A.items() :
if car in frequencia_MisraGries.keys():
frequencia_MisraGries.keys(car) = frequencia_MisraGries.values(val)
else:
frequencia_MisraGries.keys(car) = 0
return frequencia_MisraGries
def increment(caracter, A):
for k, v in A.items():
if k == caracter:
A[k]=A[v]+1
return A
清理的数据是一个列表,像这样:
['o','n','c','e','y','o',...]
值 k
从用户程序传递过来。
但是代码无法编译。我在这一行 A.keys(caracter) = 1
中有错误。
如果我以正确的方式使用代码,我不会。
您似乎正在尝试使用密钥访问字典的内容。但是,请注意 keys()
方法没有参数,从 documentation 开始,它所做的是:
This method returns a list of all the available keys in the dictionary.
如果您想使用字典的键访问字典的内容,请执行 A[caracter]
。
示例
d = {'a':3, 'b':4}
使用按键方法:
d.keys()
dict_keys(['a', 'b'])
现在,您尝试执行的操作会引发错误:
d.keys('a')
TypeError: keys() takes no arguments (1 given)
改为:
d['a']
3
在附件 link 中查找有关 Accessing Values in Dictionary 的更多信息。
我有这个伪代码:
A = empty associative array;
// Processing
while ( not end of sequence ) #referente ao data stream
j = current_token();
if ( j in keys(A) ) then A[ j ] = A[ j ] + 1;
else if ( | keys(A) | < ( k – 1 ) ) then A[ j ] = 1;
else for each i in keys(A) do
A[ i ] = A[ i ] – 1;
if ( A[ i ] == 0 ) then remove i from A;
// Output
if( a in keys(A) ) then freq_estimate = A[ a ];
else freq_estimate = 0;
我是这样实现的:
def contMisraGries(dataClean, k):
frequencia_MisraGries = {}
A={}
for caracter in dataClean:
if caracter in A.keys():
A = increment(caracter, A)
else:
if A.keys() < k-1:
A.keys(caracter) = 1
else:
for i , v in A.items():
A.keys(i) -= 1
if v == 0:
del A.items(i)
for car, val in A.items() :
if car in frequencia_MisraGries.keys():
frequencia_MisraGries.keys(car) = frequencia_MisraGries.values(val)
else:
frequencia_MisraGries.keys(car) = 0
return frequencia_MisraGries
def increment(caracter, A):
for k, v in A.items():
if k == caracter:
A[k]=A[v]+1
return A
清理的数据是一个列表,像这样:
['o','n','c','e','y','o',...]
值 k
从用户程序传递过来。
但是代码无法编译。我在这一行 A.keys(caracter) = 1
中有错误。
如果我以正确的方式使用代码,我不会。
您似乎正在尝试使用密钥访问字典的内容。但是,请注意 keys()
方法没有参数,从 documentation 开始,它所做的是:
This method returns a list of all the available keys in the dictionary.
如果您想使用字典的键访问字典的内容,请执行 A[caracter]
。
示例
d = {'a':3, 'b':4}
使用按键方法:
d.keys()
dict_keys(['a', 'b'])
现在,您尝试执行的操作会引发错误:
d.keys('a')
TypeError: keys() takes no arguments (1 given)
改为:
d['a']
3
在附件 link 中查找有关 Accessing Values in Dictionary 的更多信息。