dictionary-comprehension

Created a nested dictionary from three separate lists

烈酒焚心 提交于 2019-12-25 00:29:07
问题 I wrote some code that generates three separate lists that revolve around projects. The first list is a list of IDs, the second list is a list start dates and third list is a list of end dates. I would like to combine these lists into a single nested dictionary. item[0] from start_date and item[0] from end_date are associated with item[0] from project_id[0] project_id = ['project 1','project 2', 'project 3', 'project 4'] start_date = [datetime(2015,1,12), datetime(2015,1,13), datetime(2015,1

How to create a list of values in a dictionary comprehension in Python

别来无恙 提交于 2019-12-20 05:03:13
问题 Taking a very simple example of looping over a sentence and creating a dictionary which maps {x:y} , where x is a key representing the length of the words and y is a list of words in the sentence that contain x amount of letters Input: mywords = "May your coffee be strong and your Monday be short" Expected Output: {2: ['be', 'be'], 3: ['May', 'and'], 4: ['your', 'your'], 5: ['short'], 6: ['coffee', 'strong', 'Monday']} Here's an attempt that creates a list of values but overwrites it each

Python: Convert table to string to key:value pairs and store in dict

那年仲夏 提交于 2019-12-20 03:51:12
问题 I getting data from subprocess command as a string. I want to store this data in a dict. How best do I achieve this? Here is data example: (I have returned this as a string from subprocess.) NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 140.7M 1 loop /snap/gnome-3-26-1604/82 loop1 7:1 0 89.3M 1 loop /snap/core/6673 sda 8:0 0 11G 0 disk ├─sda1 8:1 0 10.9G 0 part / ├─sda14 8:14 0 4M 0 part └─sda15 8:15 0 106M 0 part /boot/efi Here is the output I want: {block device 1: { "name" : value,

OrderedDict comprehensions

心不动则不痛 提交于 2019-12-17 18:32:16
问题 Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict ? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from collections import OrderedDict >>> olddict, dict = dict, OrderedDict >>> {i: i*i for i in range(3)}.__class__ <type 'dict'> So, if it's possible how would I go about doing

Invalid syntax using dict comprehension

Deadly 提交于 2019-12-17 14:56:11
问题 Given a list of floats named 'x', I would like to create a dict mapping each x in x[1:-1] to it's neighbors using a dict comprehension. I have tried the following line : neighbours = {x1:(x0,x2) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:])} However, the syntax seems to be invalid. What am I doing wrong? 回答1: Dict comprehensions are only available in Python 2.7 upwards. For earlier versions, you need the dict() constructor with a generator: dict((x1, (x0,x2)) for (x0,x1,x2) in zip(x[:-2],x[1:-1

Having nodes with differing lengths of attributes, add edges if >=1 attribute is the same

回眸只為那壹抹淺笑 提交于 2019-12-13 02:58:05
问题 In NetworkX 'm trying to accomplish the following: within one graph create 'mother-nodes' and 'children-nodes', where children-nodes have only 1 attribute, and mother-nodes have several (4). create edges between mother-nodes and children-nodes if at least one attribute (key-value pair) is the same, create an edge only between a mother-node and children-node: even if two mother-nodes have one of 4 overlapping attributes, there should not be an edge between the two So far I have the first part

How to merge a list of multiple dictionaries into a dictionary of lists?

[亡魂溺海] 提交于 2019-12-12 09:53:52
问题 I have the following list of dictionaries in Python3.x: list_of_dictionaries = [{0:3523, 1:3524, 2:3540, 4:3541, 5:3542}, {0:7245, 1:7246, 2:7247, 3:7248, 5:7249, 6:7250}, {1:20898, 2:20899, 3:20900, 4:20901, 5:20902}] In this case, it's a single list with three dictionaries. I would like to efficiently merge this into a single dictionary with lists as values; here is the correct answer: correct = {0:[3523, 7245], 1:[3524, 7246, 20898], 2:[3540, 7247, 20899], 3:[7248, 20900], 4:[3541, 20901],

Dictionary comprehension for swapping keys/values in a dict with multiple equal values

萝らか妹 提交于 2019-12-12 01:13:18
问题 def invert_dict(d): inv = dict() for key in d: val = d[key] if val not in inv: inv[val] = [key] else: inv[val].append(key) return inv This is an example from Think Python book, a function for inverting(swapping) keys and values in a dictionary. New values (former keys) are stored as lists, so if there was multiple dictionary values (bound to a different keys) that were equal before inverting, then this function simply appends them to the list of former keys. Example: somedict = {'one': 1,

Convert a nested dictionary into list of tuples

那年仲夏 提交于 2019-12-11 17:14:29
问题 I have a dictionary - d={'revenues': { '201907': {'aaa.csv':'fdwe34x2'}, '201906':{'ddd.csv':'e4c5q'} }, 'complaints': {'2014': {'sfdwa.csv','c2c2jh'} } } I want to convert it into list of tuples - [ ('revenues','201907','aaa.csv','fdwe34x2'), ('revenues','201906','ddd.csv','e4c5q'), ('complaints','2014','sfdwa.csv','c2c2jh') ] I tried using list comprehensions , but did not help - l = [(k,[(p,q) for p,q in v.items()]) for k,v in d.items()] print(l) [('revenues', [('201907', {'aaa.csv':

Insert values of dict1 into dict2 but in a specific place in dict2

大城市里の小女人 提交于 2019-12-11 15:46:53
问题 I have 2 dictionaries and I want to insert the values of dict1 into dict2 but in a specific place in dict2, ie: dict1 { 'apple': 'hard tasty', 'orange': 'soft tasty', 'banana': 'soft very-tasty' } dict2 { 'apple': '<div class="apple"></div>', 'orange': '<div class="orange"></div>', 'banana': '<div class="banana"></div>' } I want to insert the values of dict1 into dict2 inside the 'class=' variable so it becomes Desired { 'apple': '<div class="apple hard tasty"></div>', 'orange': '<div class=