对于Python和Java之间的循环差异

问题描述 投票:3回答:3

在Python中我们为循环做:

for i in range(len(nums))

在java中:我们有:

for (int i = 0; i < nums.length; i++)

这两个for循环是否相同?如果我们在for循环中做了一些更改,让我们说在for循环中我已经加3,对于Java,接下来我在for循环中将是4?虽然Python仍然从2开始

Leetcode 594.最长的和谐子序列。

我们定义一个和谐数组是一个数组,其最大值和最小值之间的差值恰好为1

用Java编写的解决方案如下:

nums=[1,3,2,2,5,2,3,7]
public class Solution {
    public int findLHS(int[] nums) {
        Arrays.sort(nums);
        int prev_count = 1, res = 0;
        for (int i = 0; i < nums.length; i++) {
            int count = 1;
            if (i > 0 && nums[i] - nums[i - 1] == 1) {
                while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                    count++;
                    i++;
                }
                res = Math.max(res, count + prev_count);
                prev_count = count;
            } else {
                while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                    count++;
                    i++;
                }
                prev_count = count;
            }
        }
        return res;
    }
}

我转换为Python:

nums=[1,3,2,2,5,2,3,7]

nums=sorted(nums)
prev_count=1
res=0
i=0
for i in range(len(nums)-1):
    count=1
    if i>0 and nums[i]-nums[i-1]==1:
        while i<len(nums)-1 and nums[i] == nums[i+1]:
            count+=1
            i+=1
        res=max(res,count+prev_count)
        prev_count=count
    else:
        while i<len(nums)-1 and nums[i] == nums[i+1]:
            count+=1
            i+=1

        prev_count=count

print (res)


在Java中

for (int i = 0; i < nums.length; i++) {
            int count = 1;
            if (i > 0 && nums[i] - nums[i - 1] == 1) {
                while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                    count++;
                    i++;
                }

我在里面循环,所以我从我添加的任何东西开始。

在Python中:

for i in range(len(nums)-1):
    count=1
    if i>0 and nums[i]-nums[i-1]==1:
        while i<len(nums)-1 and nums[i] == nums[i+1]:
            count+=1
            i+=1

在i + = 1之后,它仅应用于While循环,for循环仍然从i = 2而不是4开始。

当python为4时,Java返回5的答案。我调试代码看起来像Java start i,无论我添加了什么,而Python没有添加i并始终为最后一个启动。

java python-3.x
3个回答
1
投票

在java中,for循环语义是从C语言中借用的。

for (<initilization>; <termination condition>; <what to do in after each iteration>)

在开始时做一些事情(初始化),然后到达某个条件(终止条件),做一些事情来取得进展(每次迭代后要做什么)。使用i的惯用for循环起作用,因为迭代的状态保持在i内。因此,如果在循环体中对i进行更改,则迭代状态也会发生变化。

python语法类似于bash循环:

for i in some_iterable:

这里i接受来自some_iterable的每个值,并且循环对i的每个值运行一次。如果你在循环体内改变i,无所谓;在下一次迭代期间,i被赋予迭代的下一个值。循环的状态保持在iterable中,而不是ii正是让您访问可迭代的当前值的原因。


1
投票

Python for循环与Java的增强for循环基本相同。对于你的例子,因为range(len(nums))返回[0, 1, 2, ...],这两个或多或少相等:

蟒蛇:

    array = [0, 1, 2, 3, 4, 5, 6]
    for i in array:
        // i represents each item in the array

Java的:

    int[] array = {0, 1, 2, 3, 4, 5, 6};
    for (int i : array) {
        // i represents each item in the array
    }

1
投票

这在python中不起作用 - i每次回到for i in .... :时都会“重置”

for i in range(20) :
   print(i)    # prints i
   i += 99     # has no influence over the next iterations i
   print(i)    # prints (i + 99) 

在python中解决它的方法是:

from collections import Counter

nums=[1,3,2,2,5,2,3,7]

c = Counter(nums)

# create possible keys from c that are 1 apart
one_apart_keys = [ (a, a+1) for a in c if a+1 in c]     

# get the key that has the max value of counts
# will pick first one if multiple equals possible
max_key = max(one_apart_keys, key = lambda x: c[x[0]]+c[x[1]]) 

# get all the numbers in order from list
collec = [x for x in nums if x in max_key]  

print(collec)

# c is                Counter({2: 3, 3: 2, 1: 1, 5: 1, 7: 1})
# one_apart_keys is   [(1, 2), (2, 3)]
# max_key is          (2, 3)

输出:

[3, 2, 2, 2, 3]
© www.soinside.com 2019 - 2024. All rights reserved.