dictionary

Why can't I initialize a map with new() in Go?

主宰稳场 提交于 2021-02-17 06:01:33
问题 package main import "fmt" func main() { p := new(map[string]int) m := make(map[string]int) m["in m"] = 2 (*p)["in p"] = 1 fmt.Println(m) fmt.Println(*p) } The above code gives an error panic: assignment to entry in nil map . If I print *p before inserting pairs into it, the output is correct. It seems I just can't modify *p ? 回答1: Both new and make are used to allocate memory in a program, but they work differently. new(T, args) zeros memory and returns the memory address (a value of type *T

The sum() function seems to be messing map objects

让人想犯罪 __ 提交于 2021-02-17 05:56:09
问题 I'm doing this code excercise for trying out functional programming in python, and I ran into problems with sum(), list(), and map objects. I don't understand what I'm doing wrong, but the list() function seems to be screwing with my map object. This is the code I have: people = [{'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}, {'name': 'Sam'}] heights = map(lambda x: x['height'], filter(lambda x: 'height' in x, people)) print(len(list(heights))) print(sum(list(heights)))

Python - accessing a value within in a nested dictionary within a list

♀尐吖头ヾ 提交于 2021-02-17 05:40:21
问题 I have a JSON API response that looks like the following: json_data= { "sales_list": [ { "date": "all", "country": "all", "units": { "product": { "promotions": 0, "downloads": 1, "updates": 2, "refunds": 3 }, "iap": { "promotions": 0, "sales": 0, "refunds": 0 } }, "revenue": { "product": { "promotions": "0.00", "downloads": "0.00", "updates": "0.00", "refunds": "0.00" }, "iap": { "promotions": "0.00", "sales": "0.00", "refunds": "0.00" }, "ad": "0.00" } } ], "next_page": null, "code": 200,

Combine Python List and Dict comprehension with counter

僤鯓⒐⒋嵵緔 提交于 2021-02-17 02:22:30
问题 I want to transfer a list of tuples: [(1, 3, 5), (2, 4, 6), (7, 8, 9)] to a list of dict (in order to create a pandas dataframe) which looks like: [{'index':1, 'match':1},{'index':1, 'match':3},{'index':1, 'match':5}, {'index':2, 'match':2}, {'index':2, 'match':4},{'index':2, 'match':6}, {'index':3, 'match':7},{'index':3, 'match':8},{'index':3, 'match':9}] For performance reasons I wanted to use a list and dict comprehension: [{'index':ind, 'match': } for ind, s in enumerate(test_set, 1)] How

Way to call method depending on variable?

北战南征 提交于 2021-02-17 00:54:52
问题 I already have a working, but in my oppinion not beautiful solution for a part of a long script. My script uses several similar methods, that differ too much to combine. However I came to a point where I want to call one of those methods depending on a given variable. The names of the methods are build up like this: def read_A(): #doing sth def read_B(): #doing sth else def read_C(): etc. Now I would like to call those methods in a pythonic way, when the letter ( 'A' , 'B' , 'C' , ...) is

How to reverse dictionary items and list keys grouped by common values [duplicate]

拟墨画扇 提交于 2021-02-16 18:21:48
问题 This question already has answers here : Dictionary comprehension for swapping keys/values in a dict with multiple equal values (3 answers) Closed 10 months ago . I have a dictionary that I want to group by the common values: init_dict = {'00001': 'string1', '00002': 'string2', '00003': 'string1', '00004': 'string3', '00005': 'string2'} I want to create a new dictionary that groups the values and lists the keys like this: new_dict = {'string1': ['00001', '00003'], 'string2':['00002', '00004']

Pandas Vectorized lookup of Dictionary

余生颓废 提交于 2021-02-16 18:05:42
问题 This seems like it should be a common use case but I'm not finding any good guidance on this. I have a solution that works but I would rather have a vectorized lookup rather than using the Pandas apply() function. Here is an example of what I am doing: import pandas as pd example_dict = { "category1":{ "field1": 0.0, "filed2": 5.0}, "category2":{ "field1": 5.0, "field2": 8.0}} d = {"ids": range(10), "category": ["category1" if x % 2 == 0 else "category2" for x in range(10)]} df = pd.DataFrame

Accessing Python dict keys with or without dict.keys()

偶尔善良 提交于 2021-02-16 17:53:47
问题 Usually I access dict keys using keys() method: d = {'a':1, 'b':2, 'c':3} for k in d.keys(): print k But sometimes I see this code: for k in d: print k Is this code correct? safe? 回答1: Although this was already mentioned, I wanted to add some exact numbers to these discussion. So I compared: def iter_dtest(dtest): for i in dtest: pass and def list_dtest(dtest): for i in dtest.keys(): pass A dictionary with 1 000 000 items was used (float keys) and I used timeit with 100 repetitions. These are

Remove elements from Dictionary<Key, Item>

 ̄綄美尐妖づ 提交于 2021-02-16 15:51:25
问题 I have a Dictionary, where items are (for example): "A", 4 "B", 44 "bye", 56 "C", 99 "D", 46 "6672", 0 And I have a List: "A" "C" "D" I want to remove from my dictionary all the elements whose keys are not in my list, and at the end my dictionary will be: "A", 4 "C", 99 "D", 46 How can I do? 回答1: It's simpler to construct new Dictionary to contain elements that are in the list: List<string> keysToInclude = new List<string> {"A", "B", "C"}; var newDict = myDictionary .Where(kvp=>keysToInclude

Multiple values for key in dictionary in Python

断了今生、忘了曾经 提交于 2021-02-16 10:51:55
问题 What I'm trying to do is get 3 values from a key into separate variables. Currently I'm doing it like this: for key in names: posX = names[key][0] posY = names[key][1] posZ = names[key][2] This doesn't seem very intuitive to me even though it works. I've also tried doing this: for key, value in names: location = value Unfortunately, this gives me a single object (which is what I expected), but I need the individual values assigned to the key. Thanks and apologize for my newness to Python.