算法Misra&;python中Gries的实现
我有一个伪代码:算法Misra&;python中Gries的实现,python,algorithm,pseudocode,Python,Algorithm,Pseudocode,我有一个伪代码: 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;
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;
值k
从用户程序传递
但是代码不能编译。我在这行中有错误A.keys(字符)=1
。
如果我采用正确的方式编写代码,我不知道。看起来您正试图用一个键访问字典的内容。但是,请注意,
keys()
方法不接受任何参数,因为从中可以看出,它所做的是:
此方法返回字典中所有可用键的列表
如果要使用字典的键访问字典的内容,请执行[code>a[caracter]
范例
使用“关键点”方法:
d.keys()
dict_keys(['a', 'b'])
现在,您尝试执行的操作将引发一个错误:
d.keys('a')
TypeError:keys()不接受任何参数(给定1个)
相反,你应该:
d['a']
3
在附加的链接中查找有关的更多信息
d.keys()
dict_keys(['a', 'b'])
d.keys('a')
d['a']
3