I have a list with a strings
['scene-task-v001-user', 'scene-task-v002-user', 'scene-explo-v001-user', 'scene-train-v001-user', 'scene-train-v002-user']
strings created by regular expression
'(?P<scene>\w+)-(?P<task>\w+)-v(?P<ver>\d{3,})-(?P<user>\w+)'
I need to create dictionary where key its a task group and values contain all ver groups with the same task
{'task': ['001', '002'], 'explo': ['001'], 'train': ['001', '002']}
How to do it?
Thanks!
First of all, ('t-1', 't-2', 's-1', 'z-1', 'z-2') is a tuple, not a list. In addition, {'t': {'1', '2'}, 's': {'1'}, 'z': {'1', '2'}} is wrong expression, a form of the values would be a list here, not {}. I corrected this issue in my codes below.
Instead of using regular expression, you can loop the list and split by '-' inside the loop to get keys and values, as follows:
from collections import defaultdict
l = ('t-1', 't-2', 's-1', 'z-1', 'z-2')
d = defaultdict(list)
for item in l:
key, val = item.split('-')
d[key].append(val)
print(d) # defaultdict(<class 'list'>, {'t': ['1', '2'], 's': ['1'], 'z': ['1', '2']})
print(d['t']) # ['1', '2']
Using regular expressions to get keys and values for a dictionary:
from collections import defaultdict
import re
l = ('t-1', 't-2', 's-1', 'z-1', 'z-2')
d = defaultdict(list)
for item in l:
key_patten = re.compile('\w-')
val_patten = re.compile('-\w')
key = key_patten.search(item).group().replace('-', '')
val = val_patten.search(item).group().replace('-', '')
d[key].append(val)
print(d) # defaultdict(<class 'list'>, {'t': ['1', '2'], 's': ['1'], 'z': ['1', '2']})
print(d['t']) # ['1', '2']
Related
Sorry for the post if it seems redundant. I've looked through a bunch of other posts and I can't seem to find what i'm looking for - perhaps bc I'm a python newby trying to write basic code...
Given a dictionary of any size: Some keys have a single value, others have a nested dictionary as its value.
I would like to convert the dictionary into a list (including the nested values as list items) but in a specific order.
for example:
d = {'E':{'e3': 'Zzz', 'e1':'Xxx', 'e2':'Yyy'}, 'D': {'d3': 'Vvv', 'd1':'Nnn', 'd2':'Kkk'}, 'U': 'Bbb'}
and I would like it to look like this:
order_list = ['U', 'D', 'E'] # given this order...
final_L = ['U', 'Bbb', 'D', 'd1', 'Nnn', 'd2', 'Kkk', 'd3', 'Vvv', 'E', 'e1', 'Xxx', 'e2', 'Yyy', 'e3', 'Zzz']
I can make the main keys fall into order but the the nested values. Here's what i have so far...
d = {'E':{'e3': 'Zzz', 'e1':'Xxx', 'e2':'Yyy'}, 'D': {'d3': 'Vvv', 'd1':'Nnn', 'd2':'Kkk'}, 'U': 'Bbb'}
order_list = ['U', 'D', 'E']
temp_list = []
for x in order_list:
for key,value in d.items():
if key == x:
temp_list.append([key,value])
final_L = [item for sublist in temp_list for item in sublist]
print(final_L)
My current output is:
['U', 'Bbb', 'D', {'d1': 'Nnn', 'd2': 'Kkk', 'd3': 'Vvv'}, 'E', {'e1': 'Xxx', 'e3': 'Zzz', 'e2': 'Yyy'}]
So there a couple of easy transformation to make with a list comprehension:
>>> [(k, sorted(d[k].items()) if isinstance(d[k], dict) else d[k]) for k in 'UDE']
[('U', 'Bbb'),
('D', [('d1', 'Nnn'), ('d2', 'Kkk'), ('d3', 'Vvv')]),
('E', [('e1', 'Xxx'), ('e2', 'Yyy'), ('e3', 'Zzz')])]
Now you just need to flatten an arbitrary depth list, here's a post describing how to do that:
import collections
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, str):
yield from flatten(e)
else:
yield el
>>> list(flatten((k, sorted(d[k].items()) if isinstance(d[k], dict) else d[k]) for k in 'UDE'))
['U', 'Bbb', 'D', 'd1', 'Nnn', 'd2', 'Kkk', 'd3', 'Vvv', 'E', 'e1', 'Xxx', 'e2', 'Yyy', 'e3', 'Zzz']
I have a list of lists in the following format:
[['a'],['1'],['2'],['3'], ['b'],['4'],['5'],['6']]
My desired output is:
[['a', '1'], ['a', '2'], ['a','3'],['b', '4'],['b', '5'],['b', '6']]
or even better would be:
{'a':['1','2','3'], 'b':['4','5','6']}
Essentially, the "number values" are never the same size (think that a could include 1 2 and 3, and b could include 4 5 6 7 and 8, etc)
What would be the easiest way of doing this? Using regex?
Thanks
You can use a for loop and check if the element is a digit or not:
d = {}
for i in lst:
if not i[0].isdigit(): # Check if element is a digit. If not, add a key with the current value of i[0]
d[i[0]] = []
current = i[0]
else:
d[current].append(i[0])
Output:
>>> d
{'a': ['1', '2', '3'], 'b': ['4', '5', '6']}
This is assuming everything in the list is a string
I have a question regarding how to filter a dictionary using a loop.
Here is an example of the dictionary:
d = {'beta': ['ABC', '1', '5', '10', '15'],
'lambda': ['DEF', '3', '30', '22.2', '150'],
'omega': ['RST','15', '54.4', '150', '75']
}
How do I filter the dictionary to remove keys if the 3rd value in each key is < 100? In other words, after the if function, only omega should be left in the dictionary.
I tried:
for k, v in d.iteritems():
r = float((d[key][2]))
if r < float(100):
del d[k]
But it did not work. Any thoughts? New to python programming here.
The new dictionary should just leave the omega key since 150 is greater than 100.
def cast_values(v):
try:
return float(v)
except ValueError:
return v
new_d = {k:[ cast_values(i) for i in v ] for k,v in d.items() if float(v[3]) > 100}
Results:
new_d = {'omega': ['RST', 15, 54.4, 150, 75]}
I am trying to construct a dictionary with values from a csv file.Say 10 columns there and i want to set the first column as key and the remaining as Values.
If setting as a for loop the dictionary has to have only one value. Kindly Suggest me a way.
import csv
import numpy
aname = {}
#loading the file in numpy
result=numpy.array(list(csv.reader(open('somefile',"rb"),delimiter=','))).astype('string')
#devolop a dict\
r = {aname[rows[0]]: rows[1:] for rows in result}
print r[0]
Error as follows.
r = {aname[rows[0]]: rows[1:] for rows in result}
KeyError: '2a9ac84c-3315-5576-4dfd-8bc34072360d|11937055'
I'm not entirely sure what you mean to do here, but does this help:
>>> result = [[1, 'a', 'b'], [2, 'c', 'd']]
>>> dict([(row[0], row[1:]) for row in result])
{1: ['a', 'b'], 2: ['c', 'd']}
How to unpack a list, which is nested inside another list.
Practically to transform this:
l=['aa','bb',['ccc','ddd'],'ee']
to
l=['aa','bb','ccc','ddd','ee']
See this thread and e. g. the solution of elqott
>>> from compiler.ast import flatten
>>> l = ['1','2',['3','4'],'5']
>>> flatten(l)
Following your edit
['1', '2', '3', '4', '5']
>>> l = ['aa','bb',['ccc','ddd'],'ee']
>>> flatten(l)
['aa', 'bb', 'ccc', 'ddd', 'ee']