Python evaluate mode problem instance, python instance

Source: Internet
Author: User

Python evaluate mode problem instance, python instance

This example describes how to evaluate the mode of python. 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.


Python class instantiation

You do not understand the relationship between class variables and instance variables.
In the first example, append is an operation on class variables in init. Therefore, the Instance Object newmen1/2 does not have its own variable a, and class variables are accessed.
If you display a class variable,
Print Men. a, newmen1.a, and newmen2.a are the same. Point to the same variable.
In the second example, init generates the object's own variable a. Note that '= '! When the instance object calls init, each instance object has its own variable a, and you can no longer access the class variable through the instance object. In fact, this writing method is not good.
Then you can display the class variable again,
Print Men. a, newmen1.a, newmen2
> 0 1 2
If you want all objects to share class variables, you can write
Class Men:
A = 0
Def _ init _ (self, B ):
Men. a = B
Def sayHi (self ):
Print 'hello, my name is ', Men.
Instead of using self to represent the public variables of the Instance Object. It will only make you dizzy.

Question about how to instantiate an object in python

Define a class to encapsulate all attributes, and then use the class object as the return value.
I don't know what you mean:

Class Node:
Def _ init _ (self, nodes, city, state, description = None ):
Self. nodes = nodes
Self. city = city
Self. state = state
Self. description = description

Def node_by_name (nodes, city, state ):
# Some other process
Description = 'North CAMBRIDGE'
Return Node (nodes, city, state, description)

Ans = node_by_name ('testnode', 'cambridge ', 'M ')

Print ans. state, ans. description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.