if __name__ == "__main__":
store = {}
list = ('a', 'a', 'b', 'c', 'c')
for data in list:
countoccurrences(store, data)
for k, v in store.iteritems():
print "Key " + k + " has occurred " + str(v) + " times"
代码输出
Key a has occurred 2 times
Key c has occurred 2 times
Key b has occurred 1 times
import timeit
d = {x: x**2 for x in range(1000)}
#print (d)
print (len(d))
# 1000
print (len(d.keys()))
# 1000
print (timeit.timeit('len({x: x**2 for x in range(1000)})', number=100000)) # 1
print (timeit.timeit('len({x: x**2 for x in range(1000)}.keys())', number=100000)) # 2
def dict_finder(dict_finders):
x=input("Enter the thing you want to find: ")
if x in dict_finders:
print("Element found")
else:
print("Nothing found:")