dictionary

Backspace does not seem to work in python

ぃ、小莉子 提交于 2021-02-08 07:43:47
问题 network={1:[2,3,4],2:[1,3,4], 3:[1,2], 4:[1,3,5], 5:[6,7,8], 6:[5,8],7:[5,6], 8:[5,6,7]} str1='network.csv' output = open(str1,'w') for ii1 in network.keys(): output.write(repr(ii1)+":[") for n in network[ii1]: output.write(' %s,'%(repr(n))) output.write('\b'+']\n') output.close() What I expect is something like: 1:[ 2, 3, 4] 2:[ 1, 3, 4] 3:[ 1, 2] 4:[ 1, 3, 5] 5:[ 6, 7, 8] 6:[ 5, 8] 7:[ 5, 6] 8:[ 5, 6, 7] but what I get is: 1:[ 2, 3, 4,] 2:[ 1, 3, 4,] 3:[ 1, 2,] 4:[ 1, 3, 5,] 5:[ 6, 7, 8,] 6

GeoPandas and OSMnx- plotting on map

冷暖自知 提交于 2021-02-08 06:51:19
问题 I want to plot my Geopandas df on a map. as a background i want a (road) map of the area. loving the OSMnx package, i'm trying to figure out how to use it's output (shapefile? network?) as my plot background import osmnx as ox G= ox.core.graph_from_place('Chengdu City', network_type='all_private', simplify=True, retain_all=False, truncate_by_edge=False, name='unnamed', which_result=1, buffer_dist=None, timeout=180, memory=None, max_query_area_size=2500000000, clean_periphery=True,

How do I turn a list of tuples into a dictionary while keeping redundant values?

回眸只為那壹抹淺笑 提交于 2021-02-08 06:14:32
问题 I'm getting a data set that's formatted as a list of key-value pairs. The key is the data source, and the value is the data element. For example: [('a', 3), ('b', 5), ('a', 7), ('c', 15), ('d', 12)] I want to turn this list into a dictionary. I could use Python's built-in dict() , but it throws away redundant values and keeps only the last value associated with a given key. I would like redundant values to go into a list, as follows: {'a': [3, 7], 'b': [5], 'c': [15], 'd': [12]} Is there a

Django: Most efficient way to create a nested dictionary from querying related models?

和自甴很熟 提交于 2021-02-08 04:59:37
问题 In Django, what is the most efficient way to create a nested dictionary of data from querying related and child models? For example, if I have the following models: Parent Children Pets I've seen django's model_to_dict method, and that's pretty cool, so I imagine I could loop through each level's queryset and create a bunch of DB calls on each level, for each instance, but is there a better way? For example, could "prefetch_related" be used to get all three tiers as it is used to get two

Change json.dumps dictionary values in python

﹥>﹥吖頭↗ 提交于 2021-02-08 03:58:58
问题 So i have following dict: my_dict{'key1': 'value1', 'key2': 'value2', 'key3': json.dumps([ {"**subkey1**": "subvalue1", "**subkey2**": "subvalue2",}, {"**subkey1**": "other_subvalue", "**subkey2**":"other_subvalue2"}]) } What I need is to somehow made a def where i have to check and for each subkey2 to change its value only for the def itself And all subkey1 to check if its value is the same like the second subkey1 Please note I am talking about only subkey1 which I have twice. I don't want

Flatten of dict of lists into a dataframe

橙三吉。 提交于 2021-02-08 01:50:50
问题 I have a dict of lists say: data = {'a': [80, 130], 'b': [64], 'c': [58,80]} How do I flatten it and convert it into dataframe like the one below: 回答1: Use nested list comprehension with if-else if want no count one element lists: df = pd.DataFrame([('{}{}'.format(k, i), v1) if len(v) > 1 else (k, v1) for k, v in data.items() for i, v1 in enumerate(v, 1)], columns=['Index','Data']) print (df) Index Data 0 a1 80 1 a2 130 2 b 64 3 c1 58 4 c2 80 EDIT: data = {'a': [80, 130], 'b': np.nan, 'c':

Combine multiple dictionaries into one pandas dataframe in long format

筅森魡賤 提交于 2021-02-07 21:48:18
问题 I have several dictionaries set up as follows: Dict1 = {'Orange': ['1', '2', '3', '4']} Dict2 = {'Red': ['3', '4', '5']} And I'd like the output to be one combined dataframe: | Type | Value | |--------------| |Orange| 1 | |Orange| 2 | |Orange| 3 | |Orange| 4 | | Red | 3 | | Red | 4 | | Red | 5 | I tried splitting everything out but I only get Dict2 in this dataframe. mydicts = [Dict1, Dict2] for x in mydicts: for k, v in x.items(): df = pd.DataFrame(v) df['Type'] = k 回答1: One option is using

create nested dictionary one liner

廉价感情. 提交于 2021-02-07 20:12:22
问题 Hi I have three lists and I want to create a three-level nested dictionary using one line. i.e., l1 = ['a','b'] l2 = ['1', '2', '3'] l3 = ['d','e'] I'd want to create the following nested dictionary: nd = {'a':{'1':{'d':0},{'e':0},'2':{'d':0},{'e':0},'3':{'d':0},{'e':0},'b':'a':{'1':{'d':0},{'e':0},'2':{'d':0},{'e':0},'3':{'d':0},{'e':0}} I tried using zip to do the outer loop and add the lists but elements get replaced. I.e., this does not work: nd = {i:{j:{k:[]}} for i in zip(l1,l2,l3)} 回答1

Creating a Pandas dataframe from elements of a dictionary

主宰稳场 提交于 2021-02-07 14:48:40
问题 I'm trying to create a pandas dataframe from a dictionary. The dictionary is set up as nvalues = {"y1": [1, 2, 3, 4], "y2": [5, 6, 7, 8], "y3": [a, b, c, d]} I would like the dataframe to include only "y1" and " y2" . So far I can accomplish this using df = pd.DataFrame.from_dict(nvalues) df.drop("y3", axis=1, inplace=True) I would like to know if it is possible to accomplish this without having df.drop() 回答1: You can specify columns in the DataFrame constructor: pd.DataFrame(nvalues, columns

Creating a Pandas dataframe from elements of a dictionary

爷,独闯天下 提交于 2021-02-07 14:48:03
问题 I'm trying to create a pandas dataframe from a dictionary. The dictionary is set up as nvalues = {"y1": [1, 2, 3, 4], "y2": [5, 6, 7, 8], "y3": [a, b, c, d]} I would like the dataframe to include only "y1" and " y2" . So far I can accomplish this using df = pd.DataFrame.from_dict(nvalues) df.drop("y3", axis=1, inplace=True) I would like to know if it is possible to accomplish this without having df.drop() 回答1: You can specify columns in the DataFrame constructor: pd.DataFrame(nvalues, columns