dictionary

How to find indirect relation? [Python]

谁说我不能喝 提交于 2020-12-27 07:18:31
问题 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

Simplifying `if…elif..else` conditions

落花浮王杯 提交于 2020-12-26 11:06:30
问题 If I have multiple conditions (nested and/or otherwise) with boolean (either False or True ) outputs; how could I further simplify the code and make it more efficient, comprehensive, and elegant? For example, under such circumstances as follows: if condition_1: if condition_2: # do one thing pass elif condition_3: # do another thing pass else: # do a third thing pass elif condition_2: if condition_3: # do a fourth thing pass and so on. It's a exam project of mine, so not get too much help, I

How to make Json Serialize ignore dictionary keys

五迷三道 提交于 2020-12-26 08:22:54
问题 I'm trying to serialize a dictionary within a class and the keys inside the CustomAttributes dictionary are getting formatted even though I've provided the ProcessDictionaryKeys parameter as false. I've added [JsonProperty] as shown below: [JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })] public IDictionary<string, string> CustomAttributes { get; set; } my CustomAttributes data looks like this: CustomAttributes = new

Convert list of lists to list of dictionaries

被刻印的时光 ゝ 提交于 2020-12-26 07:53:06
问题 I want to convert a list of lists to a list of dictionaries. I have a way to do it but I suspect there's a better way: t = [[1,2,3], [4,5,6]] keys = ['a', 'b', 'c'] [{keys[0]:l[0], keys[1]:l[1], keys[2]:l[2]} for l in t] with output [{'a': 1, 'c': 3, 'b': 2}, {'a': 4, 'c': 6, 'b': 5}] This could be done with a loop, but I bet there's a function to do it even easier. From this answer I'm guessing there's a way to do it with the map command, but I'm not quite sure how. 回答1: You can use list

Convert list of lists to list of dictionaries

烈酒焚心 提交于 2020-12-26 07:52:56
问题 I want to convert a list of lists to a list of dictionaries. I have a way to do it but I suspect there's a better way: t = [[1,2,3], [4,5,6]] keys = ['a', 'b', 'c'] [{keys[0]:l[0], keys[1]:l[1], keys[2]:l[2]} for l in t] with output [{'a': 1, 'c': 3, 'b': 2}, {'a': 4, 'c': 6, 'b': 5}] This could be done with a loop, but I bet there's a function to do it even easier. From this answer I'm guessing there's a way to do it with the map command, but I'm not quite sure how. 回答1: You can use list

Iterate over dictionary of objects

谁说我不能喝 提交于 2020-12-26 06:21:25
问题 I have a dictionary of objects which contains the "Names/Ranges" within a spreadsheet. As I process the spreadsheet I need to update the value associated with a range. The class to hold this info looks like this: class varName: name = None refersTo = None refersToR1C1 = None value = None def __init__(self, name, refersTo, refersToR1C1, value): self.name = name self.refersTo = refersTo self.refersToR1C1 = refersToR1C1 self.value = value I create the dictionary as follows: staticNames = {}

Using a JavaScript Map with array as key, why can't I get the stored value?

浪子不回头ぞ 提交于 2020-12-26 04:37:09
问题 My code initializes a Map object and uses arrays as key. When I try to use the map.get() method, I get "undefined" instead of the value I expect. What am I missing? const initBoardMap = () => { let theBoard = new Map() for (let r = 0; r < 3; r++) { for (let c = 0; c < 3; c++) { //create a Map and set keys for each entry an array [r,c] //set the value to a dash // ---- commented out the array as key :-( //theBoard.set([r, c], '-') const mykeyStr = r + ',' + c theBoard.set(mykeyStr, '-') } }

Using a JavaScript Map with array as key, why can't I get the stored value?

▼魔方 西西 提交于 2020-12-26 04:37:06
问题 My code initializes a Map object and uses arrays as key. When I try to use the map.get() method, I get "undefined" instead of the value I expect. What am I missing? const initBoardMap = () => { let theBoard = new Map() for (let r = 0; r < 3; r++) { for (let c = 0; c < 3; c++) { //create a Map and set keys for each entry an array [r,c] //set the value to a dash // ---- commented out the array as key :-( //theBoard.set([r, c], '-') const mykeyStr = r + ',' + c theBoard.set(mykeyStr, '-') } }

get key of max value in dictionary nodejs

痞子三分冷 提交于 2020-12-26 04:05:07
问题 I want to get the key of the max value in a dictionary using nodejs. This is what I have done but it returns the max value not the key. var b = { '1': 0.02, '2': 0.87, '3': 0.54, '4': 0.09, '5': 0.74 }; var arr = Object.keys( b ).map(function ( key ) { return b[key]; }); var max = Math.max.apply( null, arr ); console.log(max); Any idea how to do it? 回答1: const result = Object.entries(b).reduce((a, b) => a[1] > b[1] ? a : b)[0] You might just wanna work with key/value pairs to simplify this.

ValueError: Feature not in features dictionary

你。 提交于 2020-12-25 08:30:34
问题 I am attempting to write a simple deep machine learning model using TensorFlow. I'm using a toy dataset I made up in Excel just to get the model working and accepting data. My code is as follows: import pandas as pd import numpy as np import tensorflow as tf raw_data = np.genfromtxt('ai/mock-data.csv', delimiter=',', dtype=str) my_data = np.delete(raw_data, (0), axis=0) #deletes the first row, axis=0 indicates row, axis=1 indicates column my_data = np.delete(my_data, (0), axis=1) #deletes the