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.
Related
I have a map
Map<String, List> map1 = {a: [1,2,3,4], b: [5,6,7,8], c: [9,10,11]};
I know one of the values of one of the lists - 1..11.
How can I get the key of a map entity where this value belongs to - a, b, c?
Try this, where search is the value you're trying to find in the array.
Note that firstWhere can give you an error if the value cannot be found anywhere.
void main() {
final data = {
'a': [1, 2, 3, 4],
'b': [5, 6, 7, 8],
'c': [9, 10, 11]
};
final search = 4;
final selected = data.keys.firstWhere((key) {
return data[key]!.contains(search);
});
print(selected); // a
}
I found a solution
int valueToLookFor = 5;
String keyName = map1.keys.firstWhere((key) => map1[key]!.contains(valueToLookFor));
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'
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.
How to find number of matches in the array or dictionary? for example if the dictionary or the array has the number 4 occurring 5 times, how can I find out about it?
List elements:
my_list = [1,4,7,4,5,7,1,3]
print my_list.count(4)
Dictionary values using generator expression:
my_dict = {0: 1, 2: 1, 4: 5, 6: 3, 8: 4, 10: 4, 12: 1}
print sum(1 for x in my_dict.values() if x == 4)
As pointed out by zondo, the last line can be more simply written as:
print sum(x == 4 for x in my_dict.values())
due to the fact that True == 1.
For a list:
my_list = [x for x in [1,4,7,4,5,7,1,3]
len([x for x in my_list if x == 4])
For a dictionary (assuming you want to count the values):
my_dict = {0: 1, 2: 1, 4: 5, 6: 3, 8: 4, 10: 4, 12: 1}
len([x for x in my_dict.values() if x == 4])
The two code segments are counting the number of elements that match the criteria after if (in your case, that they equal 4). The len function requires a list (not a generator), requiring the extra-looking [] in there.
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