pythonfor循环和程序,C ++ for循环与Python for循环

I'm currently learning Python as I'm taking a data mining class.

I was making a for-loop to make a noisy data file to do smoothing and I found a peculiarity on Python for-loop that I couldn't understand nor go around.

So I made this simple testing C++ and Python codes.

C++ one works, but Python one doesn't.

The reason is that C++ allows arbitrary updates on the counter variable i within the for-loop block, but Python doesn't.

On Python code, I try to update i arbitrarily by doing i += 1 within the while-loop, but if you look at the outputs for At the first part of the loop, i = SOMETHING, Python is arbitrarily updating the i only in the while-loop that's in the for-loop, but then reverts the value back when it exits that while-loop.

(Outputs are in the comments at the bottom)

Why is that? Is it a scope issue?

(Both C++ and Python are statically scoped)

Is it because of their types?

(I'm only familiar with statically-typed languages like C++ and Java, and not dynamically-typed languages like Python)

On Python, it seems like the for-loop is actually a function with return-by-value parameter i which ignores all the changes on the parameter that took place inside the function.

I tried:

Setting the counter i as a global variable.

using range(0, len(input), *variable*), but I still failed to replicate it.

Researched if it can be solved by using Static variable or similar sort on Python (I think it's irrelevant?)

On Python, how would you replicate this C++ code?

Could you enlighten me on why those for-loops behave differently? Thank you.

This is C++ code that's working correctly:

#include

#include

#include

using namespace std;

int main()

{

string input = "abc defg";

string eachWord = "";

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

{

cout << "At the first part of the loop, i = " << i << " ." << endl;

while(input[i] != ' ' && input[i] != '\0')

{

eachWord += input[i];

i++;

}

cout << eachWord << endl;

cout << "At the last part of the loop, i = " << i << " ." << endl << endl;

eachWord = "";

}

}

/*

Output:

At the first part of the loop, i = 0 .

abc

At the last part of the loop, i = 3 .

At the first part of the loop, i = 4 .

defg

At the last part of the loop, i = 8 .

*/

And this is the Python code that's not working correctly, that I tried to make to replicate the C++ code:

input = "abc defg"

eachWord = ''

for i in range(len(input)):

print("At the first part of the loop, i = ", i, ".")

while(input[i] != ' ' and input[i] != '\0'):

eachWord += input[i]

i += 1

print(eachWord)

print("At the last part of the loop, i = ", i, ".")

print()

eachWord = ''

"""

Output:

At the first part of the loop, i = 0 .

abc

At the last part of the loop, i = 3 .

At the first part of the loop, i = 1 .

bc

At the last part of the loop, i = 3 .

At the first part of the loop, i = 2 .

c

At the last part of the loop, i = 3 .

At the first part of the loop, i = 3 .

At the last part of the loop, i = 3 .

At the first part of the loop, i = 4 .

Traceback (most recent call last):

File "main.py", line 6, in

while(input[i] != ' ' and input[i] != '\0'):

IndexError: string index out of range

"""

解决方案

First, replicating a c/c++ structure is not the best way to approach solving a problem. You will inevitably end up fighting against the language instead of benefiting from its strengths.

Secondly, to convert a c/c++ for loop, you have to realize that it is actually a while in disguise:

for (,,)

{

// your stuff

}

Because Python for loops override the control variable (i) at each iteration, they can not translate directly to C/C++ loops that modify the control variable within their code block. These kinds of loops translate to this in Python (which is unwieldy and cumbersome):

while :

# your stuff

for your particular example:

i = 0

while i < len(input):

# ...

i += 1

A more pythonic translation of you code would look more like this:

eachword = ''

for character in input:

if character in [' ','\0']: # will drop last word because no \0 at end of strings

print(eachword)

eachword = ''

else:

eachword += character

strings in python don't have a nul character at the end so the last word will likely be dropped unless your input comes from a non-standard source

Python has a built-in function to separate words in a string:

for eachword in input.split():

print(eachword)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值