


I am learning python recently. For a person who is deeply addicted to the C series language, there are many problems that need to be abandoned and re-understood
#coding=utf-8 global n, m, k, edge, head, dis, stack, vis, nMax, mMax, inf nMax = 100 mMax = 10000 inf = 1e+10 class e(object): pass n = 0 k = 0 m = 0 eg = e() edge = [] head = [0] dis = [0] stack = [0] vis = [0] def addedge(a, b, c): global k, edge, head ed = e() ed.u = a #you can delect it ed.v = b ed.w = c ed.next = head[a] edge.append(ed) head[a]=k k+=1 pass def spfa(): global n, m, k, edge, head, dis, stack, vis,inf i = top = 0 for i in range(0 , n): dis[i] = inf vis[i] = 0 dis[0] = 0 vis[0] = 1 top+=1 stack[top] = 0 while(top!=0): u = stack[top] top-=1 i = head[u] while(i!=0): v = edge[i].v if dis[v] > dis[u]+edge[i].w: dis[v] = dis[u]+edge[i].w if(vis[v]==0): vis[v] = 1 top+=1 stack[top] = v i = edge[i].next vis[u] = 0 pass if __name__ == '__main__': u = v = l = i = 0 for i in range(0,nMax): head.append(0); dis.append(0) vis.append(0) stack.append(0) while(1): na = input() n = int(na) ma = input() m = int(ma) edge=[0] k = 1 for i in range(0,n): head[i] = 0 for i in range(0,m): ua = input() va = input() la = input() u = int(ua) v = int(va) l = int(la) addedge(u,v,l) spfa() for i in range(1,n): print(dis[i])
Let’s talk about the problems encountered
1The length of the python list is not fixed. When you need to read an element at a fixed position, you must make sure that the position is not empty
2Python does not support "++", and the writing method of "num[index++]" in C++ does not work here
3Inputting int type values should be entered first Then use int() to force conversion
4 Global variables must be declared with global, and they must also be declared with global in the function
5 Let’s talk about a very weird thing
''' Created on 2014年7月5日 @author: bbezxcy ''' global stu,k class student: pass stu = [] def addStudent1(nm ,ag): global stu,k stu[k].name = nm stu[k].age = ag k+=1 pass def addStudent(nm ,ag): global stu,k stu[k].name = nm stu[k].age = ag k+=1 pass if __name__ == '__main__': num = 0 k = 0 strn = input("请输入学生人数") num = int(strn) ss = student() for i in range(0 ,num): stu.append(ss) for i in range(0 ,num): nm = input() ag = input() addStudent(nm ,ag) for i in range(0,num): print(stu[i].name) print(stu[i].age)
This is a simple way to insert student information into the list program. But when running, you will find that the last inserted value will overwrite the previous student information value
The printed result is as follows
请输入学生人数3 zys 20 xcy 19 ghz 20 输出结果 ghz 20 ghz 20 ghz 20
After changing addstudent to
Python code
def addStudent(nm ,ag): global stu,k s = student() s.name = nm s.age = ag stu.append(s) k+=1 pass
the problem is solved

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor