dictionary

take the first x elements of a dictionary on python

前提是你 提交于 2020-12-29 12:11:56
问题 I am a newbie on python, so I try to take the first 50 elements of a dictionary in python. I have a dictionary which is decreasing order sorted by value. k=0 l=0 for k in len(dict_d): l+=1 if l<51: print dict for a small example: dict_d={'m':'3','k':'4','g':'7','d':'9'} take the first 3 elements in a new dictionary: new_dict={'m':'3','k':'4','g':'7'} I could not find how to do that? 回答1: dict_d = {...} for key in sorted(dict_d)[:50]: print key, dict_d[key] 回答2: You can take 50 arbitrary

Use an offline interactive map in QML

不问归期 提交于 2020-12-29 10:01:31
问题 I'd like to inject a chunk of map that I got from OpenStreetMap under the form of an osm file. From what I've read in the documentation, the Open Street Map Plugin doesn't seem to have any parameter taking a source file as a map. It only works with a server. Problem is I won't have an Internet connection when running my application. I need to use a map internally. Ditto for Mapbox and HERE plugins. Is there any other way to do this? Here is the boilerplate to create a Map: Plugin { id:

C# nested dictionaries

ε祈祈猫儿з 提交于 2020-12-29 09:14:20
问题 What is wrong with my syntax? I want to be able to get the value "Genesis" with this info["Gen"]["name"] public var info = new Dictionary<string, Dictionary<string, string>> { {"Gen", new Dictionary<string, string> { {"name", "Genesis"}, {"chapters", "50"}, {"before", ""}, {"after", "Exod"} }}, {"Exod", new Dictionary<string, string> { {"name", "Exodus"}, {"chapters", "40"}, {"before", "Gen"}, {"after", "Lev"} }}}; 回答1: You cannot define a class field using var . Change var to Dictionary

C# nested dictionaries

人走茶凉 提交于 2020-12-29 09:14:05
问题 What is wrong with my syntax? I want to be able to get the value "Genesis" with this info["Gen"]["name"] public var info = new Dictionary<string, Dictionary<string, string>> { {"Gen", new Dictionary<string, string> { {"name", "Genesis"}, {"chapters", "50"}, {"before", ""}, {"after", "Exod"} }}, {"Exod", new Dictionary<string, string> { {"name", "Exodus"}, {"chapters", "40"}, {"before", "Gen"}, {"after", "Lev"} }}}; 回答1: You cannot define a class field using var . Change var to Dictionary

How to access Dictionary from other Class's function in C#?

二次信任 提交于 2020-12-29 08:48:37
问题 I have create a Class named "EngDictionary". and Then i define a dictionary in a function e.g: public void Dict() { Dictionary<string, string> d = new Dictionary<string, string>(); d.Add("Classifieds", "Kleinanzeigen"); //d.Add("windows", 5); } Now i want to access above defined dictionary from my main class for retrieving the keys and values of my Dictionary. Please Suggest me some code. I am using Visual C# 2008 Express Edition, Win Application 回答1: Declare Dictionary as class property.

Dictionary Iterating — for dict vs for dict.items()

老子叫甜甜 提交于 2020-12-29 02:54:05
问题 When we iterate over the dictionary below, each iteration returns(correctly) a key,value pair for key, value in dict.items(): print "%s key has the value %s" % (key, value) 'some key' key has the value 'some value' (repeated however many times there are a k,v pair) The above makes sense to me, however if we do this: for key in dict.items(): print "%s key has the value %s" % (key, value) ("some key", "some value") has the value "some value" (the left tuple will iterate through each key value

Dictionary Iterating — for dict vs for dict.items()

天大地大妈咪最大 提交于 2020-12-29 02:48:07
问题 When we iterate over the dictionary below, each iteration returns(correctly) a key,value pair for key, value in dict.items(): print "%s key has the value %s" % (key, value) 'some key' key has the value 'some value' (repeated however many times there are a k,v pair) The above makes sense to me, however if we do this: for key in dict.items(): print "%s key has the value %s" % (key, value) ("some key", "some value") has the value "some value" (the left tuple will iterate through each key value

How can I store a Dictionary with RealmSwift?

痴心易碎 提交于 2020-12-27 08:46:30
问题 Considering the following model: class Person: Object { dynamic var name = "" let hobbies = Dictionary<String, String>() } I'm trying to stock in Realm an object of type [String:String] that I got from an Alamofire request but can't since hobbies has to to be defined through let according to RealmSwift Documentation since it is a List<T> / Dictionary<T,U> kind of type. let hobbiesToStore: [String:String] // populate hobbiestoStore let person = Person() person.hobbies = hobbiesToStore I also

How can I store a Dictionary with RealmSwift?

谁说我不能喝 提交于 2020-12-27 08:41:53
问题 Considering the following model: class Person: Object { dynamic var name = "" let hobbies = Dictionary<String, String>() } I'm trying to stock in Realm an object of type [String:String] that I got from an Alamofire request but can't since hobbies has to to be defined through let according to RealmSwift Documentation since it is a List<T> / Dictionary<T,U> kind of type. let hobbiesToStore: [String:String] // populate hobbiestoStore let person = Person() person.hobbies = hobbiesToStore I also

How to find indirect relation? [Python]

自古美人都是妖i 提交于 2020-12-27 07:18:48
问题 So I'm trying to find indirect relations in a dictionary but I can't seem to find a general code for my program: this is what I have #find if A is related to E data = {"A": {"B": 5, "C": 7}, "B": {"E": 8}, "C": {}, "D": {}, "E": {"D": 9}} if "E" in data["A"]: result = True if "E" in data["B"] or "D" in data["C"]: result = True else: result = False print(result) #output = True because "E" is in data["A"] For this one example it works and ofcourse I've could generalize this with x's and y's but