C#のDictionary.Countプロパティ
辞書。 C#のCountプロパティは、ディクショナリ内のキーと値のペアの数を取得します。
構文
public int Count { get; } 辞書を実装する例を見てみましょう。カウントプロパティ-
例
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には次の辞書関数が含まれています- Sr.No 説明付きの関数 1 cmp(dict1、dict2) 両方のdictの要素を比較します。 2 len(dict) 辞書の全長を示します。これは、辞書内のアイテムの数と同じになります。 3 str(dict) 辞書の印刷可能な文字列表現を生成します 4 type(variable) 渡された変数のタイプを返します。渡された変数がdictionaryの場合、辞書タイプを返します。 Pythonには次の辞書メソッドが含まれています- Sr.
-
Pythonのディクショナリメソッド(update()、has_key()、fromkeys()
Pythonの辞書は、最も頻繁に使用されるコレクションデータ型の1つです。それはちょっと値のペアで表されます。キーにはインデックスが付けられていますが、値にはインデックスが付けられていない場合があります。さまざまなPythonプログラムで辞書を非常に簡単に使用できるようにするPython組み込み関数が多数あります。このトピックでは、 update()、has_key()、fromkeys()の3つの組み込みメソッドを紹介します。 。 update() メソッドupdateは、セカンダリのアイテムを最初のアイテムとマージすることにより、特定のディクショナリに新しいアイテムを追加します。 構文