在包含if语句的for循环中迭代存在问题。

huangapple go评论111阅读模式
英文:

Problem with iteration in a for loop that contains if statement

问题

我是编程新手,必须承认自己非常无知。

我目前正在尝试将Matlab脚本转换为Python,但我遇到了一些问题。我需要根据一个阈值选择现有数组的值,并创建一个新列表。阈值是一个固定值,应该不会越过数组的第i和i+1元素之间的减法结果。如果阈值被越过,我想保留第一个值(即i+1)。

以下是参考的Matlab代码:

    old_list = [10; 32; 35; 60; 90; 100];      
    doube_indices = [];    
    for i=1:length(old_list)-1
        if(old_list(i+1) - old_list(i) < threshold)
            doube_indices = [doube_indices i+1];
        end
    end
    new_list(doube_indices) = [];
    threshold = 20;

我尝试以多种方式在Python中做类似的事情,但我一直失败。我认为我的最大问题在于选择列表中的第二个数字,就像在MATLAB中一样,即i+1。这可能是一些非常简单的东西,但我对这方面的了解还不够,所以我会感激一些建议!你能帮助我吗?

英文:

I am new in programming and I have to admit very ignorant.

I am currently trying to transform a Matlab script to Python but I face some issues. What I need is to select values of an existing array, based on a threshold, and create a new list. The threshold is a set value that the result of a subtraction between the i+1 and i elements of my array should not cross. If the threshold is crossed I want to keep the first value (i.e i+1).

Here is the Matlab code of reference:

    old_list = [10; 32; 35; 60; 90; 100];      
    doube_indices = [];    
    for i=1:length(old_list)-1
        if(old_list(i+1) - old_list(i) < threshold)
            doube_indices = [doube_indices i+1];
        end
    end
    new_list(doube_indices) = [];
    threshold = 20;

I tried to make something similar in Python in several ways but I am constantly failing. I believe my biggest problem is to select the second number of the list as it is done in MATLAB i.e. i+1. It might be something very easy but I am too new on that stuff so I would appreciate some input! Could you please help me?

答案1

得分: 1

一项关键困难是Python从0开始编号其列表,而Matlab从1开始。这实现了您的基本代码:

old_list = [10, 32, 35, 60, 90, 100]
threshold = 20
double_indices = []
for i in range(len(old_list)-1):
    if old_list[i+1] - old_list[i] < threshold:
        double_indices.append(i+1)
print(double_indices)

输出:

[2, 5]

随着您变得更加熟练,您会想要开始使用numpy库。numpy实现了大部分MATLAB可以做的事情,允许您一次操作整个数组或数组切片。

后续

如果您只想要旧列表中与前一个元素不接近的值,那么这是一个不同的循环:

old_list = [10, 32, 35, 60, 90, 100]
threshold = 20
new_list = []
for val in old_list:
    if not new_list or abs(val - new_list[-1]) > threshold:
        new_list.append(val)
print(new_list)

输出:

[10, 32, 60, 90]
英文:

One key difficulty is that Python numbers its lists starting from 0, whereas Matlab starts at 1. This implements your basic code:

old_list = [10, 32, 35, 60, 90, 100]
threshold = 20
double_indices = []
for i in range(len(old_list)-1):
    if old_list[i+1] - old_list[i] &lt; threshold:
        double_indices.append(i+1)
print(double_indices)

Output:

[2, 5]

As you get more sophisticated, you'll want to start using the numpy library. numpy implements most of what MATLAB can do, allowing you to operate on an entire array or array slice at a time.

Followup

If you just want the values from the old list that are not close to the previous element, that's a different loop:

old_list = [10, 32, 35, 60, 90, 100]
threshold = 20
new_list = []
for val in old_list:
    if not new_list or abs(val - new_list[-1]) &gt; threshold:
        new_list.append(val)
print(new_list)

Output:

[10, 32, 60, 90]

huangapple
  • 本文由 发表于 2023年6月16日 06:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定