C#の辞書でキーと値のペアの数を取得します
辞書内のキーと値のペアの数を取得するためのコードは次のとおりです-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "Chris");
dict.Add("Two", "Steve");
dict.Add("Three", "Messi");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
Console.WriteLine("\nKey/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
if (dict.ContainsValue("Angelina"))
Console.WriteLine("Value found!");
else
Console.WriteLine("Value isn't in the dictionary!");
dict.Clear();
Console.WriteLine("Cleared Key/value pairs...");
foreach(KeyValuePair<string, string> res in dict){
Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
}
Console.WriteLine("Count of elements now = "+dict.Count);
}
} 出力
これにより、次の出力が生成されます-
Count of elements = 5 Key/value pairs... Key = One, Value = Chris Key = Two, Value = Steve Key = Three, Value = Messi Key = Four, Value = Ryan Key = Five, Value = Nathan Value isn't in the dictionary! Cleared Key/value pairs... Count of elements now = 0
例
別の例を見てみましょう-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "David");
dict.Add("Two", "Brian");
dict.Add("Three", "Paul");
dict.Add("Four", "Ryan");
dict.Add("Five", "Nathan");
Console.WriteLine("Count of elements = "+dict.Count);
}
} 出力
これにより、次の出力が生成されます-
Count of elements = 5
-
Pythonの辞書の値からキーを取得する
Pythonディクショナリには、キーと値のペアが含まれています。この記事では、要素の値がわかっているときにキーの値を取得することを目的としています。理想的にはキーから抽出された値ですが、ここでは逆のことを行っています。 インデックスと値付き これを実現するために、辞書コレクションのインデックス関数と値関数を使用します。最初に値を取得し、次にそこからキーを取得するリストを設計します。 例 dictA = {"Mon": 3, "Tue": 11, "Wed": 8} # list of keys and values keys = l
-
Redis HLEN –ハッシュ値に含まれるフィールドの数を取得する方法
このチュートリアルでは、コマンド– HLEN を使用して、キーに格納されているハッシュ値に含まれるフィールドの数を取得する方法について学習します。 redis-cliで。 redis HLENコマンドの構文は次のとおりです:- 構文:- redis host:post> HLEN <key name> 出力:- - (integer) value, representing the number of fields in the hash.- 0, if key does not exist.- Error, if key exist and value store