C#のOrderedDictionaryから指定されたキーのエントリを削除します
指定されたキーのエントリをC#のOrdererdDictionaryから削除するには、コードは次のとおりです-
例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary elements...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
dict.Remove("E");
Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
}
} 出力
これにより、次の出力が生成されます-
OrderedDictionary elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear Count of elements in OrderedDictionary = 6 Count of elements in OrderedDictionary (Updated)= 5
例
別の例を見てみましょう-
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "One");
dict.Add("B", "Two");
dict.Add("C", "Three");
dict.Add("D", "Four");
Console.WriteLine("OrderedDictionary elements...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count);
dict.Remove("C");
dict.Remove("D");
Console.WriteLine("Count of elements in OrderedDictionary (Updated)= " + dict.Count);
}
} 出力
これにより、次の出力が生成されます-
OrderedDictionary elements... A One B Two C Three D Four Count of elements in OrderedDictionary = 4 Count of elements in OrderedDictionary (Updated)= 2
-
Python-辞書からキーを削除する方法
辞書は、日中のプログラミング、Web開発、AI / MLプログラミングなどのさまざまな実用的なアプリケーションでも使用されており、全体として便利なコンテナになっています。したがって、辞書の使用に関連するさまざまなタスクを実行する方法を知っていることは常にプラスです。 例 # using del # Initializing dictionary test_dict = {"Vishesh" : 29, "Ram" : 21, "Vishal" : 27, "Prashant" : 25} # Printing di
-
Python辞書からキーを削除する方法は?
Pythonのdelキーワードは、ほとんどすべてのオブジェクトで使用されます。辞書から特定のアイテムを削除するには、delステートメントにキー句を指定します >>> D1 = {1: a, 2: b, 3: c, x: 1, y: 2, z: 3} >>> del D1[x] >>> D1 {1: a, 2: b, 3: c, y: 2, z: 3} キーと値のペアを削除する効果は、pop()メソッドでも実現できます。メソッドはキーを取得します(同じキーに複数の値が割り当てられている場合はオプションで値を取得します) >>