0
我需要編寫一個函數,它將第一個參數和一個整數作爲第二個參數,並返回所有值大於第二個參數的鍵列表。我正在考慮使用for循環來構建它更簡單。編寫字典函數
我需要編寫一個函數,它將第一個參數和一個整數作爲第二個參數,並返回所有值大於第二個參數的鍵列表。我正在考慮使用for循環來構建它更簡單。編寫字典函數
你使用哪種語言?你使用什麼樣的字典?
如果Python中,使用以下命令:
[ x for x, y in mydict.items() if y > 42 ]
這是有幫助嗎?
KeysOverX() - 你可以移植,如果你想,因爲我們不知道你想要:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DictionaryQuestion
{
class Program
{
static void Main(string [] args) {
// Define dictionary
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "lorum");
dict.Add(2, "ipsum");
dict.Add(3, "this");
dict.Add(4, "is");
dict.Add(5, "a");
dict.Add(6, "test");
// Define
int startKey = 4;
var results = KeysOverX(dict, startKey);
foreach (int k in results) {
Console.WriteLine(k);
}
}
static IList<int> KeysOverX(Dictionary<int, string> dictionary, int lowestKey) {
return (from item in dictionary
where item.Key > lowestKey
select item.Key).ToList<int>();
}
}
}
有趣的語言。前進! ;) – user492238 2011-03-30 11:44:54
什麼語言? 「這樣的字典」是什麼意思?這是功課嗎? (我最好的猜測是:Python;它是作業;「這樣的字典」指的是你被要求做的事情的前一部分。)如果它是Python:你知道列表解析嗎?如果沒有'for'循環,你可以更加整潔地做到這一點。 – 2011-03-30 11:45:19
這是什麼問題?一旦你有問題,我們需要知道你在寫什麼語言。 – drysdam 2011-03-30 11:45:55