This article describes examples of the python evaluate mode, including file read/write, dictionary usage, and numerical calculation techniques, for more information about how to evaluate the mode of python, see the example in this article. it is a typical application. Share it with you for your reference. The details are as follows:
Problem description:
The element with the largest number of duplicates is called the crowd... it is a set with repeated elements. the number with the most duplicates in this set is called its crowd...
For example, if S = [1, 2, 2, 3, 5], the number of duplicates is 2, and the number of duplicates is 3.
The instance code is as follows:
List_num = [] list_num_count = 0dict_num = {}# number of elements in the first behavior set of the file read from the file, and then each row is an element list_num_count = int(open('input.txt ', 'r '). readline () for line_num, line in enumerate (open ("input.txt", 'r'): if line_num> 0: list_num + = line. split () # add the read element to the dictionary for item in list_num: if dict_num.has_key (item): dict_num [item] + = 1 else: dict_num.setdefault (item, 1) pass # find the maximum number of occurrences and find the number of duplicates dict_sort_by_top = {} top_value = 0for valus in dict_num.itervalues (): if valus> top_value: top_value = valus pass # Find the mode based on the number of duplicates... this is because multiple elements may have the same number of duplicates. the_pop_num = 0the_pop_num_count = 0for keys, values in dict_num.iteritems (): if values = top_value: print 'The pop num is % s, and the appear num is % s' % (keys, values) the_pop_num = keys the_pop_num_count = values # output to the file, the first behavior is from the number, the second behavior involves write_line = '% s \ n % s' % (the_pop_num, the_pop_num_count) open ("output.txt", 'w '). write (write_line)
The contents of the directory input.txt are as follows:
811372372459937
8 in the first row indicates the number of elements, and each row has an element.
The test environment is Python2.7.6,
The pythonprogram runs the input.txt file as follows:
the pop num is 37,and the appear num is 3
The output.txt file is generated at the same time to record the mode 37 and the number of repetitions 3.
I hope this article will help you with Python programming.