Why the output is different in this list comprehension? - python-3.x

When I Execute the following code it works correctly:
hassam="CHECK"
list1={i:hassam[i] for i in range(5)}
list1
output:
{0: 'C', 1: 'H', 2: 'E', 3: 'C', 4: 'K'}
but when i execute this:
hassam="CHECK"
list1={hassam[i]:i for i in range(5)}
list1
output:
{'C': 3, 'H': 1, 'E': 2, 'K': 4}
why isnt this:
{'C': 1, 'H': 2, 'E': 3,'C' : 4 ,'K': 5}

For the dictionary :
{0: 'C', 1: 'H', 2: 'E', 3: 'C', 4: 'K'}
the numbers being the key is not same.
But for the dictionary
{'C': 1, 'H': 2, 'E': 3,'C' : 4 ,'K': 5}
python doesn't allow duplicate keys. Therefore the key is updated with the new value.
Here, it showed so because dictionaries cannot have same keys with diffrent values. So try using list1 as a list like:
list1=[{hassam[i]:i} for i in range(5)]
This would give:
[{'C': 0}, {'H': 1}, {'E': 2}, {'C': 3}, {'K': 4}]
Or a tuple instead of individual dictionaries:
list1=[(hassam[i],i) for i in range(5)]

Related

How can we include only specific json fields for comparision in deepdiff pyhton?

Here, we have 2 JSON, I want to include only 'id' field for comparison, rest of the fields should be ignored.
j1 = {'MyList': [{'a': 1, 'b': 2, 'c': [{'id': '1'}]},
{'a': 1, 'b': 2, 'c': [{'id': '2'}]}], "j": "2222"}
j2 = {'MyList': [{'a': 1, 'b': 2, 'c': [{'id': '4'}]},
{'a': 1, 'b': 2, 'c': [{'id': '7'}]}], "j": "7777"}
Please suggest, How we can achieve this using DeepDiff.

How to calculate count of occurence of each value from a list column of Pandas efficiently?

I have a Pandas data frame, which looks like the following:
df =
col1
['a', 'b']
['d', 'c', 'a']
['b', 'f', 'a']
col1 is a list column, which contains strings. I want to calculate value counts of each element, which may occur in any of the lists in any row. Expected output is a dictionary, of counts of each value
Expected Output
df_dict = {'a': 3, 'b': 2, 'c': 1, 'd': 1, 'f': 1}
How to do this efficiently in 1 line preferably to make the code clean. Sorry, if it has been answered before.
With explode and value_counts:
df['col1'].explode().value_counts().to_dict()
Output:
{'a': 3, 'b': 2, 'd': 1, 'f': 1, 'c': 1}

Return multiple lines in a for loop

d = {'U': 4, '_': 2, 'C': 2, 'K': 1, 'D': 4, 'T': 6, 'Q': 1, 'V': 2, 'A': 9, 'F': 2, 'O': 8, 'J': 1, 'I': 9, 'N': 6, 'P': 2, 'S': 4, 'M': 2, 'W': 2, 'E': 12, 'Z': 1, 'G': 3, 'Y': 2, 'B': 2, 'L': 4, 'R': 6, 'X': 1, 'H': 2}
def __str__(self):
omgekeerd = {}
for sleutel, waarde in self.inhoud.items():
letters = omgekeerd.get(waarde, '')
letters += sleutel
omgekeerd[waarde] = letters
for aantal in sorted(omgekeerd):
return '{}: {}'.format(aantal, ''.join(sorted(omgekeerd[aantal])))
I need to return the value, followed by a ':' and then followed by every letter that has that value.
The problem is that when I use return, it only returns one value instead of every vale on a new line.
I can't use print() because that is not supported by the method str(self).
The return statement ends function execution and specifies a value to
be returned to the function caller.
I believe that your code is terminated too early because of wrong usage of return statement.
What you could do is to store what you would like to return in a seperate list/dictionary and then when everything is done, you can return the new dict/list that you've stored the results in.
If I understood you correctly; This is what might be looking for:
def someFunc():
d = {'U': 4, '_': 2, 'C': 2, 'K': 1, 'D': 4, 'T': 6, 'Q': 1, 'V': 2, 'A': 9,
'F': 2, 'O': 8, 'J': 1, 'I': 9, 'N': 6, 'P': 2, 'S': 4, 'M': 2, 'W': 2, 'E': 12,
'Z': 1, 'G': 3, 'Y': 2, 'B': 2, 'L': 4, 'R': 6, 'X': 1, 'H': 2}
result = {}
for key, value in d.iteritems():
result[value] = [k for k,v in d.iteritems() if v == value]
return result
# call function and iterate over given dictionary
for key, value in someFunc().iteritems():
print key, value
Result:
1 ['K', 'J', 'Q', 'X', 'Z']
2 ['C', 'B', 'F', 'H', 'M', 'P', 'W', 'V', 'Y', '_']
3 ['G']
4 ['D', 'L', 'S', 'U']
6 ['N', 'R', 'T']
8 ['O']
9 ['A', 'I']
12 ['E']

is there a simple way i can convert this list into a dictionary (python)

the list is this :
List1 = ['a','b','c','d','e','f','g','h','h','i','j','k','l','m','n']
And I am hoping for the outcome to be where each times the item appears in the list its assigned an integer e.g:
List1 = ['a:1']
without using the 'import counter' module
You could use this list comprehension:
dict((x, List1.count(x)) for x in set(List1))
Example output:
{'d': 1, 'f': 1, 'l': 1, 'c': 1, 'j': 1, 'e': 1, 'i': 1, 'a': 1, 'h': 2, 'b': 1, 'm': 1, 'n': 1, 'k': 1, 'g': 1}
(Edited to match edited question.)
Use a dictionary comprehension and count.
>>> List1 = ['a','b','c','d','e','f','g','h','h','i','j','k','l','m','n']
>>> mapping = {v: List1.count(v) for v in List1}
>>> mapping
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1,
'g': 1, 'h': 2, 'i': 1, 'j': 1, 'k': 1, 'l': 1, 'm': 1, 'n': 1}

Accessing values in nested dictionaries

I am trying to write a function that:
Counts the number of "sub-keys" of a key (e.g. 'e' is a sub-key of 'M')
Divides each sub-key's probability by the total number of sub-keys for that key
Modifies the numbers in place.
My function has to return None, however.
For example, if I had the dictionary:
defaultdict(dict, {'M': {'e': 1.0}, 'O': {'n': 2, 'x': 1.0}, 'I': {'_': 1.0, 's': 1}, 'P': {'t': 3}, 'L': {'ne': 1, 'n': 1.0}})
So for the example dictionary the converted dictionary output would be:
defaultdict(<class 'dict'>, {'M': {'e': 1.0}, 'O': {'n': 0.6666666666666666, 'x': 0.3333333333333333}, 'I': {'_': 0.5, 's': 0.5}, 'P': {'t': 1.0}, 'L': {'ne': 0.5, 'n': 0.5}})
Another example, if I had the dictionary:
defaultdict(dict, {('H', 't'): {'m': 2}, ('M', 'o'): {'ce': 1, 'p': 2}, ('K', '^'): {'d': 2}, ('F', 'x'): {'_': 1, 'g': 3}, ('J', 'o'): {'y': 1}, ('A', 'b'): {'k': 3}, ('X', '_'): {'r': 1}, ('N', 'e'): {'x': 1}})
The converted dictionary would be:
defaultdict(<class 'dict'>, {('M', 'o'): {'ce': 0.3333333333333333, 'p': 0.6666666666666666}, ('K', '^'): {'d': 1.0}, ('F', 'x'): {'g': 0.75, '_': 0.25}, ('J', 'o'): {'y': 1.0}, ('H', 't'): {'m': 1.0}, ('A', 'b'): {'k': 1.0}, ('X', '_'): {'r': 1.0}, ('N', 'e'): {'l': 1.0}})
How would I go about doing this? How do I access keys within a defaultdict of dictionaries. What I am currently thinking is:
for major_key in dictionary:
dictionary[major_key]...
...and that's where I get stuck.
Any help would be greatly appreciated!
Aside from being able to iterate over just the keys in dictionaries, in Python you can also iterate over key-value tuples , or just values. So we could do something like this:
for major_key, sub_dict in d.items():
total_key_count = 0
# Add up all sub key values to calculate probabilities
for sub_count in sub_dict.values():
total_key_count += sub_count
# Now update nested dictionary values accordingly
for minor_key, sub_count in sub_dict.items():
sub_dict[minor_key] = sub_count / total_key_count
That should help give you the results you want. Note in Python 2 you should call iteritems() instead of items(), since in Python 2 items() actually builds a list of tupes, rather than returning an iterator like iteritems() does. Same with values() and itervalues().

Resources