#python +java# the same function code in two language implementations

Source: Internet
Author: User

Overview: Set up two arrays/lists, List A is [three-to-one], List B is [' A ', ' B ', ' C '], put them on one-to print, but do not print "3:c" this pair.

Python version:

"' Created on 2014-11-2@author:administrator '" Class Myarray:def __init__ (self): SELF.A = range (1,4) se        lf.b = [' A ', ' B ', ' C '] def getValue (self): TMP = {} for I in Self.a:tmp[str (i)] = Self.b[i-1] return Tmpa = MyArray () x = A.getvalue () for i in x:if i = = ' 3 ': Continue print '%s is:%s '% (I,x[i])


Java Edition:

public class MyArray {public static void main (String[]args) {int[] a = new int[3];char[] b = new char[]{' A ', ' B ', ' C '};for (in T i=0;i<3;i++) {a[i] = i+1;} for (int i=0;i<b.length;i++) {if (i==2) {continue;} System.out.println (a[i]+ "is:" +b[i]);}}


About dictionary formatting in the Python version:

Reference:

In [1]: Phonebook = {: 123,: 456,: 789}in [2]:% phonebookout[2]:

It is not possible to use: "%s is:% (key)"% (string,dict) format dictionary format string, and in the preceding case, the dictionary key must be a string, otherwise it cannot be formatted.

When writing the Python version, I suddenly realized that I had forgotten something: return causes the function to return a value and stop the iteration or loop, so the code is problematic:

def error (list): For i in list:if xxx:return xxx #只能输出一个值


#python +java# the same function code in two language implementations

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.