python_dictionaries
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python_dictionaries [2023/01/01 16:09] – [tasks] raju | python_dictionaries [2023/08/02 19:41] (current) – [find key corresponding to a value] raju | ||
---|---|---|---|
Line 4: | Line 4: | ||
* [[convert dict of dicts to list of dicts]] | * [[convert dict of dicts to list of dicts]] | ||
* [[Convert a dictionary of dataframes to a big dataframe]] | * [[Convert a dictionary of dataframes to a big dataframe]] | ||
+ | * [[Convert a string to a dictionary]] | ||
+ | |||
+ | |||
+ | ==== find key corresponding to a value ==== | ||
+ | The following will find the first key corresponding to a given value | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | 1 | ||
+ | </ | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | 2 | ||
+ | </ | ||
+ | |||
+ | Sample use case: | ||
+ | * In https:// | ||
==== initialize a dictionary ==== | ==== initialize a dictionary ==== | ||
Line 14: | Line 39: | ||
Out[1]: | Out[1]: | ||
{' | {' | ||
+ | </ | ||
+ | |||
+ | ==== dump dictionary to file ==== | ||
+ | You can use pprint (stands for pretty print) | ||
+ | |||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.9 | packaged by conda-forge | (main, Jan 11 2023, 15:15:40) [MSC v.1916 64 bit (AMD64)] | ||
+ | IPython 8.8.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | import pprint | ||
+ | cats = [{' | ||
+ | pprint.pformat(cats) | ||
+ | Out[1]: | ||
+ | " | ||
+ | |||
+ | In [2]: | ||
+ | fileObj = open(' | ||
+ | fileObj.write(' | ||
+ | fileObj.close() | ||
+ | |||
+ | In [3]: | ||
+ | exit() | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | $ cat myCats.py | ||
+ | cats = [{' | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.9 | packaged by conda-forge | (main, Jan 11 2023, 15:15:40) [MSC v.1916 64 bit (AMD64)] | ||
+ | IPython 8.8.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | import myCats | ||
+ | myCats.cats | ||
+ | Out[1]: | ||
+ | [{' | ||
+ | |||
+ | In [2]: | ||
+ | myCats.cats[0] | ||
+ | Out[2]: | ||
+ | {' | ||
+ | |||
+ | In [3]: | ||
+ | myCats.cats[0][' | ||
+ | Out[3]: | ||
+ | ' | ||
+ | |||
+ | In [4]: | ||
+ | exit() | ||
+ | </ | ||
+ | |||
+ | Ref:- | ||
+ | * pprint is discussed in https:// | ||
+ | |||
+ | ==== merge python dictionaries ==== | ||
+ | |||
+ | To merge two python dictionaries | ||
+ | < | ||
+ | def merge_two_dicts(x, | ||
+ | ''' | ||
+ | Given two dicts, merge them into a new dict as a shallow copy. | ||
+ | For common keys, the values in y take precedence over values in x. | ||
+ | ''' | ||
+ | z = x.copy() | ||
+ | z.update(y) | ||
+ | return z | ||
+ | </ | ||
+ | |||
+ | Sample usage: | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | {' | ||
+ | </ | ||
+ | |||
+ | |||
+ | To merge an undefined number of dicts | ||
+ | < | ||
+ | def merge_dicts(*dict_args): | ||
+ | ''' | ||
+ | Given any number of dicts, shallow copy and merge into a new dict, | ||
+ | precedence goes to key value pairs in latter dicts. | ||
+ | ''' | ||
+ | result = {} | ||
+ | for dictionary in dict_args: | ||
+ | result.update(dictionary) | ||
+ | return result | ||
+ | </ | ||
+ | |||
+ | Given dicts a to g | ||
+ | < | ||
+ | z = merge_dicts(a, | ||
+ | </ | ||
+ | will give a new dict z with all the key-value pairs. If same key exists in multiple dictionaries, | ||
+ | |||
+ | Ref:- http:// | ||
+ | |||
+ | ==== Convert a dictionary to a string ==== | ||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.6 | packaged by conda-forge | (main, Oct 24 2022, 16:02:16) [MSC v.1916 64 bit (AMD64)] | ||
+ | Type ' | ||
+ | IPython 8.4.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | d = {' | ||
+ | |||
+ | In [2]: | ||
+ | import json | ||
+ | s = json.dumps(d) | ||
+ | |||
+ | In [3]: | ||
+ | d | ||
+ | Out[3]: | ||
+ | {' | ||
+ | |||
+ | In [4]: | ||
+ | s | ||
+ | Out[4]: | ||
+ | ' | ||
+ | |||
+ | In [5]: | ||
+ | type(d) | ||
+ | Out[5]: | ||
+ | dict | ||
+ | |||
+ | In [6]: | ||
+ | type(s) | ||
+ | Out[6]: | ||
+ | str | ||
</ | </ | ||
python_dictionaries.1672589365.txt.gz · Last modified: 2023/01/01 16:09 by raju