I have this problem where I have 2 dictionaries. One dict has keys representative of names (i.e. sample 1, sample 2, etc.) and the values are coordinates. The second dict has keys that are coordinates, and the values are data for each coordinate.
I need to build a 3rd dict that takes the names (keys), and then retrieves the respective values from the 2nd dict. As seen below:
dict1 = {32.0: [[(1, 7)], [(1, 17)], [(1, 8)], [(1, 18)]]}
dict2 = {(1, 7): 25.746392, (1, 18): 19.4782, (1, 17): 21.7492, (1, 8): 34.492}
dict3 = {32.0: [[25.746392], [21.7492], [34.492], [19.4782]]}
There are many more data points. I'm not sure if this is a simple problem, or if I'm having issues due to how nested the first dict is.
I'm still relatively new to Python, so any help is appreciated!
Two nested for-loops:
dict3 = dict1
for k in dict3.keys():
for i in range(len(dict3[k])):
dict3[k][i] = [dict2[dict3[k][i][0]]]
print dict3
Output:
{32.0: [[25.746392], [21.7492], [34.492], [19.4782]]}
One-liner:
dict3 = {k: [[dict2[dict1[k][i][0]]] for i in range(len(dict1[k]))] for k in dict1.keys()}
Related
I'm still learning Julia, and I recently came across the following code excerpt that flummoxed me:
res = (; [(:x, 10), (:y, 20)]...) # why the semicolon in front?
println(res) # (x = 10, y = 20)
println(typeof(res)) # NamedTuple{(:x, :y), Tuple{Int64, Int64}}
I understand the "splat" operator ..., but what happens when the semicolon appear first in a tuple? In other words, how does putting a semicolon in (; [(:x, 10), (:y, 20)]...) create a NamedTuple? Is this some undocumented feature/trick?
Thanks for any pointers.
Yes, this is actually a documented feature, but perhaps not a very well known one. As the documentation for NamedTuple notes:
help?> NamedTuple
search: NamedTuple #NamedTuple
NamedTuple
NamedTuples are, as their name suggests, named Tuples. That is, they're a tuple-like
collection of values, where each entry has a unique name, represented as a Symbol.
Like Tuples, NamedTuples are immutable; neither the names nor the values can be
modified in place after construction.
Accessing the value associated with a name in a named tuple can be done using field
access syntax, e.g. x.a, or using getindex, e.g. x[:a]. A tuple of the names can be
obtained using keys, and a tuple of the values can be obtained using values.
[... some other non-relevant parts of the documentation omitted ...]
In a similar fashion as to how one can define keyword arguments programmatically, a
named tuple can be created by giving a pair name::Symbol => value or splatting an
iterator yielding such pairs after a semicolon inside a tuple literal:
julia> (; :a => 1)
(a = 1,)
julia> keys = (:a, :b, :c); values = (1, 2, 3);
julia> (; zip(keys, values)...)
(a = 1, b = 2, c = 3)
As in keyword arguments, identifiers and dot expressions imply names:
julia> x = 0
0
julia> t = (; x)
(x = 0,)
julia> (; t.x)
(x = 0,)
dict = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
Second is 'b' with 4 times. Joint third are 'c' and 'd' with 3 times. As the dictionary changes over time, say, 'f' is added with a value of 3, then third will be 'c', 'd', and 'f' with 3 times.
just create a gencomp of tuple value,key and sort it. Print items
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
x = sorted(((v,k) for k,v in d.items()))
print(x[-2][1])
print(x[-3][1])
result:
b
d
(would fail if dict doesn't have at least 3 items)
Or directly with key parameter (avoids data reordering)
x = sorted(d.items(),key=(lambda i: i[1]))
print(x[-2][0])
print(x[-3][0])
result:
b
d
BTW avoid using dict as a variable.
EDIT: since there are several identical values, you may want to get the 2 second best values and the associated letters. You have to do it differently. I'd create a default list using key as value and store in a list, then sort it as done in the above code:
import collections
d = {'a':5 , 'b':4, 'c':3, 'd':3, 'e':1}
dd = collections.defaultdict(list)
for k,v in d.items():
dd[v].append(k)
x = sorted(dd.items())
print(x[-2])
print(x[-3])
result:
(4, ['b'])
(3, ['c', 'd'])
I am having trouble writing python code to print how many available seats there are in a row. I have a simple set of nested dictionaries - what I need to add is a test to see how many consecutive keys == available. the output needs to be something like, 'there is a block of 4 seats available together'.
seats = {'A': ['available', 'unavailable','available','available','available'],
'B': ['unavailable', 'unavailable','available','available','available'],
'C': ['available', 'unavailable','unavailable','available','available'],
'D': ['available', 'unavailable','available','unavailable','unavailable']}
Is there a very simple method of counting how many items in a row have the same value? I am literally brand new to programming, so I really need explanation of how code is working as well.
One way -- which may seem over-powered for this problem, but it's a handy tool to learn -- is to use itertools.groupby. Its job is to group consecutive terms of a sequence together, which is exactly what you want to do here. For example, if we had a list made of up 1 and 2:
>>> from itertools import groupby
>>> groupby([1,2,2,1,2,2,2,1,1,2])
<itertools.groupby object at 0x8ee793c>
>>> [(key, list(group)) for key, group in groupby([1,2,2,1,2,2,2,1,1,2])]
[(1, [1]), (2, [2, 2]), (1, [1]), (2, [2, 2, 2]), (1, [1, 1]), (2, [2])]
We can make a list of key-value pairs where the groups are the value. (Really they're an iterable grouper object, so we have to take the list to materialize them to see them.)
So in your case:
>>> groupby(seats["A"])
<itertools.groupby object at 0x8ee7e64>
>>> [(k, list(g)) for k,g in groupby(seats["A"])]
[('available', ['available']), ('unavailable', ['unavailable']), ('available', ['available', 'available', 'available'])]
>>> [len(list(g)) for k,g in groupby(seats["A"]) if k == 'available']
[1, 3]
>>> max(len(list(g)) for k,g in groupby(seats["A"]) if k == 'available')
3
With a little more work we could get the locations too, if those were significant. We can use enumerate to give the seats numbers:
>>> list(enumerate(seats["A"]))
[(0, 'available'), (1, 'unavailable'), (2, 'available'), (3, 'available'), (4, 'available')]
And then group these instead, using the key parameter to groupby to tell it we want to group on the second term (index #1), the available/unavailable state, not the number:
>>> grouped = groupby(enumerate(seats["A"]), key=lambda x: x[1])
and then extract the contiguous available seats:
>>> avail = [list(g) for k,g in grouped if k == 'available']
>>> avail
[[(0, 'available')], [(2, 'available'), (3, 'available'), (4, 'available')]]
From this we can do all sorts of things.
>>> min(avail, key=len) # one of the smallest groups
[(0, 'available')]
>>> max(avail, key=len) # one of the largest groups
[(2, 'available'), (3, 'available'), (4, 'available')]
>>> max(avail, key=len)[0][0] # start of largest
2
>>> next(g for g in avail if len(g) >= 2)
[(2, 'available'), (3, 'available'), (4, 'available')]
Here's a quick and dirty code block that should help get you started:
if rowChoice not in seats:
# Tell the user they entered a bad row and keep prompting them for input until it works
max_available = 0
available = 0
for chair in seats[rowChoice]:
# When a seat is taken, update the maxium number of available seats and reset our counter
if chair != "available":
if available > max_available:
max_available = available
available = 0
else:
available += 1
print "There is a maximum block of %d seats available" % (max_available)
Suppose I have a tuple of (1, 2, 3) and want to index a multidimensional array with it such as:
index = (1, 2, 3)
table[index] = 42 # behaves like table[1][2][3]
index has an unknown number of dimensions, so I can't do:
table[index[0]][index[1]][index[2]]
I know I could do something like this:
functools.reduce(lambda x, y: x[y], index, table)
but it's utterly ugly (and maybe also inefficient), so I wonder if there's a better, more Pythonic choice.
EDIT: Maybe a simple loop is best choice:
elem = table
for i in index:
elem = elem[i]
EDIT2: Actually, there's a problem with both solutions: I can't assign a value to the indexed array :-(, back to ugly:
elem = table
for i in index[:-1]:
elem = elem[i]
elem[index[-1]] = 42
The question is very interesting and also your suggested solution looks good (havn't checked it, but this kind of problem requires a recursive treatment and you just did it in one line).
However, the pythonic way I use in my programs is to use dictionaries of tuples. The syntax is array-like, the performance - of a dictionary, and there was no problem in it for me.
For example:
a = {(1, 2, 3): 'A', (3, 4, 5): 'B', (5, 6, 7, 8): 'C'}
print a[1, 2, 3]
print a[5, 6, 7, 8]
Will output:
A
B
And assigning to an index is super easy:
a[1, 4, 5] = 42. (But you might want to first check that (1, 4, 5) is within the dict, or else it will be created by the assignment)
I have a CSV file of data like this:
1, [a, b, c]
2, [a, b, d]
3, [a]
and some Plone objects which should be updated like this:
ID, LinesField
a, [1,2,3]
b, [1,2]
c, [1]
d, [2]
So, to clarify, the object with the id a is named on lines 1, 2 and 3 of the CSV, and thus the LinesField property of object a needs to have those line ids (the first number on the line) listed.
Ideally I'd like to use Transmogrifier to import this information (and avoid doing any manipulation in Excel beforehand), and I can see two ways, theoretically of doing this, but I can't work out how to do this in practice. I'd be grateful for some pointers to examples. I think that either I need to transform the entire pipeline so that the items reflect the structure of my Plone objects and then use the ATSchemaUpdater blueprint, but I can't see any examples on how to add items to the pipeline (do I need to write my own blueprint?) Or, alternatively I could loop through the items as they exist and append the value in the left column to the items in the list in the right. For that I need a way of appending values with ATSchemaUpdater rather than overwriting them - again, is there a blueprint for that anywhere?
Here's a few sample csv lines:
"Name","Themes"
"Bessie Brown","cah;cab;cac"
"Fred Blogs","cah;cac"
"Dinah Washington","cah;cab"
The Plone object will be a theme and the lines field a list of names:
cah, ['Bessie Brown', 'Fred Boggs' etc etc]
I'm not pretty sure you want to read the CVS file using transmogrifier, but I think you can create a section to insert these values to the items in the pipeline using a function like this:
def transpose(cvs):
keys = []
[keys.extend(v) for v in cvs.values()]
keys = set(keys)
d = {}
for key in keys:
values = [k for k, v in cvs.iteritems() if key in v]
d[key] = values
return d
In this context, cvs is {1: ['a', 'b', 'c'], 2: ['a', 'b', 'd'], 3: ['a']}; keys will contain all possible values set(['a', 'c', 'b', 'd']); and d will be what you want {'a': [1, 2, 3], 'c': [1], 'b': [1, 2], 'd': [2]}.
Probably there are better ways to do it, but I'm not a Python magician.
The insert section could look like this one:
class Insert(object):
"""Insert new keys into items.
"""
classProvides(ISectionBlueprint)
implements(ISection)
def __init__(self, transmogrifier, name, options, previous):
self.previous = previous
self.new_keys = transpose(cvs)
def __iter__(self):
for item in self.previous:
item.update(self.new_keys)
yield item
After that you can use the SchemaUpdater section.