dictionary

List of dictionaries with comprehension in python

ⅰ亾dé卋堺 提交于 2021-02-18 11:22:31
问题 I have the following list of dictionaries: ld=[{'a':10,'b':20},{'p':10,'u':100}] I want to write a comprehension like this: [ (k,v) for k,v in [ d.items() for d in ld ] ] basically I want to iterate over dictionaries in the list and get the keys and values of each dict and do sth. Example: One example output of this would be for example another list of dictionaries without some keys: ld=[{'a':10,'b':20},{'p':10,'u':100}] new_ld=[{'a':10},{'p':10}] However, the above comprehension is not

How should python dictionaries be stored in pytables?

こ雲淡風輕ζ 提交于 2021-02-18 10:40:53
问题 pytables doesn't natively support python dictionaries. The way I've approached it is to make a data structure of the form: tables_dict = { 'key' : tables.StringCol(itemsize=40), 'value' : tables.Int32Col(), } (note that I ensure that the keys are <40 characters long) and then create a table using this structure: file_handle.createTable('/', 'dictionary', tables_dict) and then populate it with: file_handle.dictionary.append(dictionary.items()) and retrieve data with: dict(file_handle

Displaying nested dictionary in Jinja2

一世执手 提交于 2021-02-18 08:16:25
问题 I have the following Jinja2 template: {% block body %} {% for key in tree recursive %} {% set outer_loop = loop %} {% for subkey in tree[key] %} {% if subkey == 'R' %} {{ tree[key][subkey] }} {% else %} {{ outer_loop(dict([(subkey, tree[key][subkey])])) }} {% endif %} {% endfor %} {% endfor %} {% endblock body %} where tree is a Python dictionary such as: tree = {"A": {"R": [1, 2, 3], "B": {"R": [4, 5, 6]}}} and dict() is the Python library function. The issue is that the template displays

PyEnchant without German dictionary

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-18 07:11:21
问题 Thanks to Stackoverflow, I learnt about pyenchant library. I am looking for German dictionary "de_DE" to use through enchant, but could not find one. Where can I get it and which directory should I put it so that pyenchant can see it? I am using a linux box running Ubuntu. 回答1: From the terminal type: sudo apt-get install myspell-de-de You can check that you have it available, from a Python prompt type: import enchant print enchant.list_languages() To check it works, from a Python prompt type

PyEnchant without German dictionary

99封情书 提交于 2021-02-18 07:09:18
问题 Thanks to Stackoverflow, I learnt about pyenchant library. I am looking for German dictionary "de_DE" to use through enchant, but could not find one. Where can I get it and which directory should I put it so that pyenchant can see it? I am using a linux box running Ubuntu. 回答1: From the terminal type: sudo apt-get install myspell-de-de You can check that you have it available, from a Python prompt type: import enchant print enchant.list_languages() To check it works, from a Python prompt type

PyEnchant without German dictionary

三世轮回 提交于 2021-02-18 07:06:03
问题 Thanks to Stackoverflow, I learnt about pyenchant library. I am looking for German dictionary "de_DE" to use through enchant, but could not find one. Where can I get it and which directory should I put it so that pyenchant can see it? I am using a linux box running Ubuntu. 回答1: From the terminal type: sudo apt-get install myspell-de-de You can check that you have it available, from a Python prompt type: import enchant print enchant.list_languages() To check it works, from a Python prompt type

PyEnchant without German dictionary

不想你离开。 提交于 2021-02-18 07:05:20
问题 Thanks to Stackoverflow, I learnt about pyenchant library. I am looking for German dictionary "de_DE" to use through enchant, but could not find one. Where can I get it and which directory should I put it so that pyenchant can see it? I am using a linux box running Ubuntu. 回答1: From the terminal type: sudo apt-get install myspell-de-de You can check that you have it available, from a Python prompt type: import enchant print enchant.list_languages() To check it works, from a Python prompt type

Modify dict values inplace

懵懂的女人 提交于 2021-02-17 19:16:12
问题 I would like to apply a function to values of a dict inplace in the dict (like map in a functional programming setting). Let's say I have this dict : d = { 'a':2, 'b':3 } I want to apply the function divide by 2.0 to all values of the dict, leading to: d = { 'a':1., 'b':1.5 } What is the simplest way to do that? I use Python 3 . Edit: A one-liner would be nice. The divide by 2 is just an example, I need the function to be a parameter. 回答1: You may find multiply is still faster than dividing

Beginner Python: AttributeError: 'list' object has no attribute

折月煮酒 提交于 2021-02-17 12:24:26
问题 The error says: AttributeError: 'list' object has no attribute 'cost' I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles: class Bike(object): def __init__(self, name, weight, cost): self.name = name self.weight = weight self.cost = cost bikes = { # Bike designed for children" "Trike": ["Trike", 20, 100], # Bike designed for everyone" "Kruzer": ["Kruzer", 50, 165] } When I try to calculate profit with my for statement, I get the

Beginner Python: AttributeError: 'list' object has no attribute

烈酒焚心 提交于 2021-02-17 12:24:13
问题 The error says: AttributeError: 'list' object has no attribute 'cost' I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles: class Bike(object): def __init__(self, name, weight, cost): self.name = name self.weight = weight self.cost = cost bikes = { # Bike designed for children" "Trike": ["Trike", 20, 100], # Bike designed for everyone" "Kruzer": ["Kruzer", 50, 165] } When I try to calculate profit with my for statement, I get the