How to build a map from two lists using zip? - python-2.7

here is my list:
projects = ["A", "B", "C"]
hours = [1,2,3]
I want my final answer to be like: {A:1,B:2,C:3}
Is there any suggestion?

Did you try to call dict constructor?
dict(zip(projects,hours))
The code fragment zip(projects,hours) will generate a list of tuples (key,value) which will be used to feed the map (usually called dictionary in python) constructor: dict

In Python 2.7 is also "dictionary comprehension"
>>> projects = ["A", "B", "C"]
>>> hours = [1,2,3]
>>> res = {project: hours for project, hours in zip(projects, hours)}
>>> res
... {'A': 1, 'B': 2, 'C': 3}

My answer is {'A': 1, 'C': 3, 'B': 2}, but I want it to be exactly {'A': 1, 'B': 2, 'C': 3}. I used "sorted", but it only printed out "A, B, C", which missed the value of dictionary

Related

change values of a dictionary in a list, while preserving the existing list

list1 = [{'a': 'a1', 'b': 'b1', 'c': 'c1'}]
if I want to change 'a1' value to a2, how can i do that?
also, with changing a1 to a2, I also have to preserve list1 and make new list.
there are no any questions about this in this site....
There are not lists, they are dictionaries. You don't need the square brackets.
dict1 = {'a': 'a1', 'b': 'b1', 'c': 'c1'}
To copy a dictionary use .copy()
dict2 = dict1.copy()
To replace the value of keyname a use.
dict2['a'] = 'a2'
print(dict1)
print(dict2)
Output:
{'a': 'a1', 'b': 'b1', 'c': 'c1'}
{'a': 'a2', 'b': 'b1', 'c': 'c1'}
If, for some reason, you do really want your dictionary inside a list, you can modify the values by addressing it's position in the list and then the dictionary key. In your case, there is one dictionary with index 0 in the list.
dict2[0]['a'] = 'a2'

Syntax to create a dict or set in comprehensions

In Python 2.7.15:
>>> dict((s, len(s)) for s in ['aa', 'b', 'cccc'])
{'aa': 2, 'cccc': 4, 'b': 1}
>>> {(s, len(s)) for s in ['aa', 'b', 'cccc']}
set([('b', 1), ('aa', 2), ('cccc', 4)])
Why does dict() create a dictionary, but {} creates a set? I thought those two syntaxes were synonymous.

Python2 : Create a list of dictionaries from two list of values

I have two input lists as following:
x_values = [1,2,3]
y_values = [1,2,3]
Is there a quick way to create a list of dictionaries from these two lists, like following:
points = [{x=1, y=1},{x=2, y=2},{x=3, y=3}]
Thanks in advance.
If I understood the question this should help:
>>> x_values = [1, 2, 3]
>>> y_values = [1, 2, 3]
>>> points = [{"x":i, "y":j} for i, j in zip(x_values, y_values)]
>>> points
[{'y': 1, 'x': 1}, {'y': 2, 'x': 2}, {'y': 3, 'x': 3}]
There could be a more Pythonic way to do this, but one straight forward approach could be:
x_values = [1,2,3]
y_values = [1,2,3]
points = []
i = 0
while i < len(x_values):
new_dict = {}
new_dict['y'] = y_values[i]
new_dict['x'] = x_values[i]
points.append(new_dict)
i += 1
This may at least get you going.

Wrong mapping of key, value in python dictionary

For code below:
a = dict()
x = ['a','b','c']
y = [1,2,3,4,5,6]
z = ['p','q']
for zz in z:
a[zz] = dict((xx,yy) for xx,yy in zip(x,(t for t in y)))
print a
I am getting:
{'q': {'a': 1, 'c': 3, 'b': 2}, 'p': {'a': 1, 'c': 3, 'b': 2}}
But, the dictionary I am expecting is:
{'q': {'a': 4, 'c': 6, 'b': 5}, 'p': {'a': 1, 'c': 3, 'b': 2}}
Am I doing something wrong? Is there any logical error in the code?
Each time through your for block, the t for t in y (which I assume was just an attempt to make something work when y alone didn't work?) is started over, so you're always going to get the first elements from the y list. One way to get it to keep its position each time through would be to turn it into an iterator:
y = iter([1,2,3,4,5,6])
While you're at it, you might as well turn the (t for t in y) back into simply y.
What about:
a = dict()
x = ['a','b','c']
y = [1,2,3,4,5,6]
z = ['p','q']
for i, zz in enumerate(z):
a[zz] = dict((xx,yy) for xx,yy in zip(x*(i+1),y))
print a
In the original code, the zip(x, (t for t in y)) part will always cut the end off of the longer of the two lists.

Python way to sort tuples in a dictionary

Could someone explain how dictionaries are sorted and why?
The below line's output:
>>> d= {(1, 2):"f", (1, 3):"f", (1, 4):"f", (1, 4):"f"}
>>> d
{(1, 2): 'f', (1, 5): 'f', (1, 3): 'f', (1, 4): 'f'}
and in general :
>>> de= {"a":1, "b":1, "c":1, "e":1, "d":1}
>>> de
{'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1}
Lists don't behave like this so I'm confused. This is more out of curiosity I could sort it myself for example.
They're hashtables, so they don't guarantee any sorting in any way. After all, that's why they're fast.
Dictionaries are not sorted. The language spec does not garantee that if you print a dictionary twice, that the order will be the same.
Lists on the other hand are indeed sorted.
If you want to emulate something like a dictionary with a given order of the keys, then you could use a list over tuples of keys and values.
dictionaries are key/value pairs. you can slice the dictionaries index and values into a list and sort them if you want to iterate the dictionary in sorted order. Supposing you wanted to see the dictionaries values in sorted order