选取Python中列表元素的阈值值

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

Picking the threshold value for list elements in Python

问题

Cb = [[0.001,
7.557543577091797e-06,
1656.90]]

threshold_Cb = 10.0

if Cb > threshold_Cb:
Cb = threshold_Cb
print(Cb)

The error is

in <module>
    if Cb > threshold_Cb:

TypeError: '>' not supported between instances of 'list' and 'float'

The expected output is

Cb=[[0.001,
  7.557543577091797e-06,
  10.0]]
英文:

I have a list Cb. If any element of Cb is greater than threshold_Cb, I want to be that element equal to threshold_Cb. But I am getting error. I present the expected output.

Cb=[[0.001,
  7.557543577091797e-06,
  1656.90]]

threshold_Cb=10.0

if Cb > threshold_Cb:
    Cb = threshold_Cb
    print(Cb)

The error is

in <module>
    if Cb > threshold_Cb:

TypeError: '>' not supported between instances of 'list' and 'float'

The expected output is

Cb=[[0.001,
  7.557543577091797e-06,
  10.0]]

答案1

得分: 1

你可以使用:

new_Cb = [[x if x <= 阈值_Cb else 阈值_Cb for x in Cb[0]]]

像上面的理解方式比循环更高效。Cb[0]是必需的,因为你有一个嵌套的列表,上面的[[ ... ]]是因为你想要一个嵌套的列表。

得到:

[[0.001, 7.557543577091797e-06, 10.0]]
英文:

You can use:

new_Cb = [[x if x &lt;= threshold_Cb else threshold_Cb for x in Cb[0]]]

Comprehensions like the above are more efficient than loops. Cb[0] is required because you have a nested List and the [[ ...]] in the above is because you want a nested List.

gives

[[0.001, 7.557543577091797e-06, 10.0]]

答案2

得分: 1

您有一个嵌套列表(也称为列表中的列表)。当您运行您的代码时,它正在迭代外部列表(Cb)。这意味着它将尝试将 [0.001, 7.557543577091797e-06, 1656.90] 与您的阈值(threshold_Cb=10.0)进行比较,这是您错误的根源。
您需要的是迭代 [0.001, 7.557543577091797e-06, 1656.90] 中的元素,并将它们与 threshold_Cb 进行比较。

1) 解决方案:

Cb=[[0.001,
    7.557543577091797e-06,
    1656.90]]

threshold_Cb=10.0

for inner_list in Cb:
    for index, element in enumerate(inner_list): 
        if element > threshold_Cb:
            inner_list[index] = threshold_Cb

print(Cb)
[[0.001, 7.557543577091797e-06, 10.0]]
英文:

You have a nested list (aka a list inside a list). When you run your code, it is iterating over the outer list (Cb). This means that will try to compare

[0.001,
7.557543577091797e-06,
1656.90]
with your threshold (threshold_Cb=10.0), and this is the source of your error.
What you need is to iterate over elements in [0.001, 7.557543577091797e-06, 1656.90] and compare them to threshold_Cb.

1) Solution:

   Cb=[[0.001,
      7.557543577091797e-06,
      1656.90]]
    
    threshold_Cb=10.0
    
    for inner_list in Cb:
        for index, element in enumerate(inner_list): 
            if element &gt; threshold_Cb:
                inner_list[index] = threshold_Cb
    
    
    print(Cb)
[[0.001, 7.557543577091797e-06, 10.0]]

答案3

得分: 0

需要迭代遍历嵌套列表中的每个项目,并逐个进行比较:

Cb=[[0.001,
  7.557543577091797e-06,
  1656.90]]
  
threshold_Cb=10.0
# 遍历嵌套列表中的第一个列表
for index, value  in enumerate(Cb[0]):
    # 检查值是否大于阈值
    if value > threshold_Cb:
        # 用阈值重新赋值该值
        Cb[0][index] = threshold_Cb
print(Cb)

输出结果:

[[0.001, 7.557543577091797e-06, 10.0]]

否则,你的 "if" 语句将比较嵌套列表是否大于整数,这不是一个有意义的比较。因此,你会收到错误消息。

当 Cb 包含多个列表时,你可以这样做:

# 遍历 Cb 中的嵌套列表
for lst_index, lst in enumerate(Cb):
    # 遍历嵌套列表中的值
    for value_index, value  in enumerate(lst):
        # 检查值是否大于阈值
        if value > threshold_Cb:
            # 用阈值重新赋值该值
            Cb[lst_index][value_index] = threshold_Cb
英文:

You need to iterate over every item in the nested list and compare them individually:

Cb=[[0.001,
  7.557543577091797e-06,
  1656.90]]

threshold_Cb=10.0
# Iterating over the first list within the nested list you have
for index, value  in enumerate(Cb[0]):
    # checking if the value is above treshold
    if value &gt; threshold_Cb:
        # Reassigning the value with index
        Cb[0][index] = threshold_Cb
print(Cb)

Output:

[[0.001, 7.557543577091797e-06, 10.0]]

Otherwise your "if" statement is comparing if a nested list is larger than an integer and that is not a meaningful comparison. Hence your error message.

When Cb has more than one lists inside it you can do this:

# iterating over nested lists inside Cb
for lst_index, lst in enumerate(Cb):
    # Iterating over the nested list values
    for value_index, value  in enumerate(lst):
        # checking if the value is above treshold
        if value &gt; threshold_Cb:
            # Reassigning the value with index
            Cb[lst_index][value_index] = threshold_Cb

答案4

得分: 0

如果您想要在原地修改Cb而不是创建一个新列表,我建议:

Cb = [
    [
        0.001,
        7.557543577091797e-06,
        1656.90
    ]
]

threshold_Cb = 10.0

for e in Cb:
    e[:] = [min(v, threshold_Cb) for v in e]

print(Cb)

输出:

[[0.001, 7.557543577091797e-06, 10.0]]

注意:

简洁的代码(如此代码)不一定是最高效的,这绝对是其中之一。

英文:

If you want to modify Cb in situ rather than create a new list I suggest:

Cb = [
    [
        0.001,
        7.557543577091797e-06,
        1656.90
    ]
]

threshold_Cb = 10.0

for e in Cb:
    e[:] = [min(v, threshold_Cb) for v in e]

print(Cb)

Output:

[[0.001, 7.557543577091797e-06, 10.0]]

Note:

Concise code (such as this) isn't necessarily most efficient and this is certainly one of those cases

huangapple
  • 本文由 发表于 2023年6月15日 18:08:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481414.html
匿名

发表评论

匿名网友

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

确定