Dictionary にC#で指定されたキーがあるかどうかを確認します
Dictionary
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<string, string> dict =
new Dictionary<string, string>();
dict.Add("One", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
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.ContainsKey("Three"))
Console.WriteLine("Key found!");
else
Console.WriteLine("Key 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 = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Key found! 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", "John");
dict.Add("Two", "Tom");
dict.Add("Three", "Jacob");
dict.Add("Four", "Kevin");
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.ContainsKey("mykey"))
Console.WriteLine("Key found!");
else
Console.WriteLine("Key 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 = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan Key isn't in the dictionary! Cleared Key/value pairs... Count of elements now = 0
-
Pythonを使用して、指定されたベースで数値の0が連続しているかどうかを確認します
数値に特定の基数のゼロが連続しているかどうかを確認する必要がある場合は、数値と基数をパラメーターとして受け取り、別のメソッドを使用して、基数が存在するかどうかに応じてYesまたはNoを返すメソッドが定義されます。 以下は同じのデモンストレーションです- 例 def check_consecutive_zero(N, K): my_result = convert_to_base(N, K) if (check_n(my_result)): print("Yes")
-
与えられた番号がPythonでEuclid番号であるかどうかを確認します
数nがあるとします。 nがユークリッド数であるかどうかを確認する必要があります。私たちが知っているように、ユークリッド数は整数であり、として表すことができます n =P n +1 ここで、は最初のn個の素数の積です。 したがって、入力がn =211のような場合、出力はTrueになります。nはとして表すことができます。 211 =(2×3×5×7)+1 これを解決するには、次の手順に従います- MAX:=10000 primes:=新しいリスト 関数generate_all_primes()を定義します。これには時間がかかります prime:=サイズMAXのリストとT