void main (){
print(occurence("hello hi hello one two two three"));
}
occurence(text){
var words = text.split(" ");
print(words);
var count = {};
words.map((element) => {
if (count[element]){
count[element] =1
}else{
count[element] =1
}
});
return count;
}
我想得到這個輸出: {hello:2, hi:1, one:1, two:2, three:1}
我的代碼哪里出了問題,我在運行程式時才得到 {}。
uj5u.com熱心網友回復:
您應該像這樣使用更新功能:
void main (){
print(occurence("hello hi hello one two two three"));
}
occurence(text){
var words = text.split(" ");
print(words);
var count = {};
for (var word in words) {
count.update(word, (value) => value 1, ifAbsent: () => 1);
}
return count;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522605.html
標籤:镖发现-出现
上一篇:顫振中是否有并發串列
