Related
Given a list of integers:
Write codes to count the frequency of each integer, and then print each integer with its frequency. (The order of the elements does not matter)
For example, for n = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6], the print out will be:
1 3
2 2
3 3
4 5
5 3
6 9
And make each two numbers in a row.
i.e. in the first row, 3 is the frequency of 1 in the list n
My code is like that, and it shows 'builtin_function_or_method' object is not subscriptable:
n = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6]
def question_7_1(n=n):
count=0
for i in n:
if i==n.index[i]:
count=count+1
print(i,count)
n = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6]
def question_7_1(n):
dict ={}
for j in n:
count=1
#print(j)
if j in dict.keys():
count = dict[j]
count=count+1
dict[j] = count
else:
dict[j] = count
dict[j] = count
#print(dict)
return dict
result = question_7_1(n)
print(result)
output :
{1: 3, 2: 2, 3: 3, 4: 5, 5: 3, 6: 9}
Implementing it in a comparatively easy to understand method.
n = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6]
freq = {}
for i in n:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
print ("Count is :\n "+ str(freq))
This should return the integer and the count.
The error in your code is the use of n.index[i]. It should've been n.index(i).
Output:
Count is :
{1: 3, 3: 3, 2: 2, 4: 5, 5: 3, 6: 9}
For a better implementation, refer to the answer by #Sheri!
Tons of ways of doing this some ways are:
Solution1:
n = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6]
def question_7_1(n):
myCountDict = {}
for i in n:
countOfaNum = n.count(i)
myCountDict[i] = countOfaNum
print(myCountDict)
question_7_1(n)
Solution2:
More effective and optimal:
a = [1,1,1,3,3,3,2,2,4,4,4,4,4,5,5,5,6,6,6,6,6,6,6,6,6]
d = {x:a.count(x) for x in a}
print(d)
Output:
{
1: 3,
2: 2,
3: 3,
4: 5,
5: 3,
6: 9
}
I have a dictionary:
d = {1: 2,
2:3,
3: 4,
5:6,
6:7}
I want this output:
d= { 1: 4 , 2 :4 , 3:4 , 5:7 , 6:7}
basically 2 is parent of 1, 3 is parent of 2, 4 is parent of 3. I want to say that 1,2 and 3 is related to 4
For each key-value pair in the dict, you can use a while loop to keep checking if the current value is a valid key in the dict, and make the value of that key the new value, until the value is not a key, at which point you have found the leaf and you can assign that value to the current key:
for k, v in d.items():
while v in d:
v = d[v]
d[k] = v
so that given:
d = {1: 2,
2: 3,
3: 4,
5: 6,
6: 7}
d becomes:
{1: 4, 2: 4, 3: 4, 5: 7, 6: 7}
I want a data structure to store 3 things - book name, score given to the book by a user, and a count of how many users have given that book the unique score:
BookName Score Count
Book1: 7 1
4 2
Book2: 5 1
9 1
3 2
2 1
Book3: 9 3
Book4: 8 1
9 1
4 2
Which data structure allows me to store this info and access them easily?
I tried using dictionaries for this, like so:
my_dict = [{'BookName':'Book1', 'Score': 7, 'NumberOfTimes':2},
{'BookName':'Book2', 'Score': 4, 'NumberOfTimes':1},
{'BookName':'Book1', 'Score': 5, 'NumberOfTimes':3}]
But when I want to display all instances of Book1, it shows only the first instance:
my_dict['BookName'=='Book1']
>>> {'BookName': 'Book1', 'NumberOfTimes': 2, 'Score': 7}
So, which is the best data structure, or what is the best way, to do this?
I would suggest you to have a data structure as follows:
book_dict = {
'Book1': [{'NumberOfTimes':2, 'Score':7}, {'NumberOfTimes':3, 'Score':5}],
'Book2': [{'NumberOfTimes':1, 'Score':5}]
}
this way you can access all the instances of a particular book using book_dict['Book1']
You will also be able to iterate over a book.
I would rather keep it simple and use the below:
book_dict = {"Book1" : {7:1,4:2}, "Book2" : {5:1,9:1,3:2,2:1}}
for k,v in book_dict.items():
print (k, v)
book_dict["Book1"][7] = book_dict["Book1"][7]+1
print (book_dict["Book1"])
book_dict["Book1"][8] = 1
print (book_dict["Book1"])
Result:
Book1 {7: 1, 4: 2}
Book2 {5: 1, 9: 1, 3: 2, 2: 1}
{7: 2, 4: 2}
{7: 2, 4: 2, 8: 1}
d = {10:"x", 1:"wx", 2:"yz"}
a = d.setdefault(1)
b = d.setdefault(3)
s = "{}" * len(d)
print(s.format(*d))
Why will the output be 11032?
After 2 setdefault calls,
d = {10: "x", 1: "wx", 2: "yz"}
d.setdefault(1) # does not change the dictionary because there's already 1
d.setdefault(3) # add 3 with value None (default if not speicfied)
d becomes:
>>> d
{10: 'x', 1: 'wx', 2: 'yz', 3: None}
Iterating dictionary yields dictionary keys: 10, 1, 2, 3. (iteration performed by *d to unpack the argument d):
>>> for key in d:
... print(key)
...
10
1
2
3
So, s.format(*d) is equivalent to '{}{}{}{}'.format(10, 1, 2, 3).
I know we use enumerate for iterating a list but I tried it on a dictionary and it didn't give an error.
CODE:
enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
for i, key in enumerate(enumm):
print(i, key)
OUTPUT:
0 0
1 1
2 2
3 4
4 5
5 6
6 7
Can someone please explain the output?
On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.
The normal case you enumerate the keys of the dictionary:
example_dict = {1:'a', 2:'b', 3:'c', 4:'d'}
for i, k in enumerate(example_dict):
print(i, k)
Which outputs:
0 1
1 2
2 3
3 4
But if you want to enumerate through both keys and values this is the way:
for i, (k, v) in enumerate(example_dict.items()):
print(i, k, v)
Which outputs:
0 1 a
1 2 b
2 3 c
3 4 d
The first column of output is the index of each item in enumm and the second one is its keys. If you want to iterate your dictionary then use .items():
for k, v in enumm.items():
print(k, v)
And the output should look like:
0 1
1 2
2 3
4 4
5 5
6 6
7 7
Just thought I'd add, if you'd like to enumerate over the index, key, and values of a dictionary, your for loop should look like this:
for index, (key, value) in enumerate(your_dict.items()):
print(index, key, value)
dict1={'a':1, 'b':'banana'}
To list the dictionary in Python 2.x:
for k,v in dict1.iteritems():
print k,v
In Python 3.x use:
for k,v in dict1.items():
print(k,v)
# a 1
# b banana
Finally, as others have indicated, if you want a running index, you can have that too:
for i in enumerate(dict1.items()):
print(i)
# (0, ('a', 1))
# (1, ('b', 'banana'))
But this defeats the purpose of a dictionary (map, associative array) , which is an efficient data structure for telephone-book-style look-up. Dictionary ordering could be incidental to the implementation and should not be relied upon. If you need the order, use OrderedDict instead.
enumerate() when working on list actually gives the index and the value of the items inside the list.
For example:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i, j in enumerate(list):
print(i, j)
gives
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
where the first column denotes the index of the item and 2nd column denotes the items itself.
In a dictionary
enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
for i, j in enumerate(enumm):
print(i, j)
it gives the output
0 0
1 1
2 2
3 4
4 5
5 6
6 7
where the first column gives the index of the key:value pairs and the second column denotes the keys of the dictionary enumm.
So if you want the first column to be the keys and second columns as values, better try out dict.iteritems()(Python 2) or dict.items() (Python 3)
for i, j in enumm.items():
print(i, j)
output
0 1
1 2
2 3
4 4
5 5
6 6
7 7
Voila
Since you are using enumerate hence your i is actually the index of the key rather than the key itself.
So, you are getting 3 in the first column of the row 3 4even though there is no key 3.
enumerate iterates through a data structure(be it list or a dictionary) while also providing the current iteration number.
Hence, the columns here are the iteration number followed by the key in dictionary enum
Others Solutions have already shown how to iterate over key and value pair so I won't repeat the same in mine.
Iterating over a Python dict means to iterate over its keys exactly the same way as with dict.keys()
The order of the keys is determined by the implementation code and you cannot expect some specific order:
Keys and values are iterated over in an arbitrary order which is
non-random, varies across Python implementations, and depends on the
dictionary’s history of insertions and deletions. If keys, values and
items views are iterated over with no intervening modifications to the
dictionary, the order of items will directly correspond.
That's why you see the indices 0 to 7 in the first column. They are produced by enumerate and are always in the correct order. Further you see the dict's keys 0 to 7 in the second column. They are not sorted.
That sure must seem confusing. So this is what is going on. The first value of enumerate (in this case i) returns the next index value starting at 0 so 0, 1, 2, 3, ... It will always return these numbers regardless of what is in the dictionary. The second value of enumerate (in this case j) is returning the values in your dictionary/enumm (we call it a dictionary in Python). What you really want to do is what roadrunner66 responded with.
Python3:
One solution:
enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7}
for i, k in enumerate(enumm):
print("{}) d.key={}, d.value={}".format(i, k, enumm[k]))
Output:
0) enumm.key=0, enumm.value=1
1) enumm.key=1, enumm.value=2
2) enumm.key=2, enumm.value=3
3) enumm.key=4, enumm.value=4
4) enumm.key=5, enumm.value=5
5) enumm.key=6, enumm.value=6
6) enumm.key=7, enumm.value=7
An another example:
d = {1 : {'a': 1, 'b' : 2, 'c' : 3},
2 : {'a': 10, 'b' : 20, 'c' : 30}
}
for i, k in enumerate(d):
print("{}) key={}, value={}".format(i, k, d[k])
Output:
0) key=1, value={'a': 1, 'b': 2, 'c': 3}
1) key=2, value={'a': 10, 'b': 20, 'c': 30}
You may find it useful to include index inside key:
d = {'a': 1, 'b': 2}
d = {(i, k): v for i, (k, v) in enumerate(d.items())}
Output:
{(0, 'a'): True, (1, 'b'): False}
d = {0: 'zero', '0': 'ZERO', 1: 'one', '1': 'ONE'}
print("List of enumerated d= ", list(enumerate(d.items())))
output:
List of enumerated d= [(0, (0, 'zero')), (1, ('0', 'ZERO')), (2, (1, 'one')), (3, ('1', 'ONE'))]