在Python中将值以特定格式追加到列表

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

Appending values in a specific format to a list in Python

问题

false_indices = [[]]

for i in range(len(MaxPrnew)):  
    Cond1 = MaxPrnew[i] > Pr_cap_t[i]
    Cond_t.append(Cond1) 
    if not Cond1: 
        false_indices.append(i)

print("false_indices =", false_indices)
英文:

I have two lists. I am writing a condition to find index values when MaxPrnew > Pr_cap_t and appending it as shown below. I present the current and expected outputs.

Pr_cap_t=[5051.061086369891,
 5046.295040175788,
 5072.5758312061325,
 5055.713625152885,
 5056.625327627877]

MaxPrnew=[241.08095445711552,
 1726.9903228626436,
 1232.262749719154,
 484.3360494454877,
 1232.262749719154]


Cond_t=[]
false_indices = [[]]

for i in range(len(MaxPrnew)):  
    Cond1 = MaxPrnew[i] > Pr_cap_t[i]
    Cond_t.append(Cond1) 
    if not Cond1: 
        false_indices.append(i)

print("false_indices =",false_indices)

The current output is

false_indices = [[], 0, 1, 2, 3, 4]

The expected output is

false_indices = [[], [0, 1, 2, 3, 4]]

答案1

得分: 0

Pr_cap_t=[5051.061086369891,
 5046.295040175788,
 5072.5758312061325,
 5055.713625152885,
 5056.625327627877]

MaxPrnew=[241.08095445711552,
 1726.9903228626436,
 1232.262749719154,
 484.3360494454877,
 1232.262749719154]

k = [x for x,y in enumerate(zip(Pr_cap_t, MaxPrnew)) if y[0] > y[1]] 

false_indices = [[]]

false_indices.append(k)

false_indices
#[[], [0, 1, 2, 3, 4]]
英文:
Pr_cap_t=[5051.061086369891,
 5046.295040175788,
 5072.5758312061325,
 5055.713625152885,
 5056.625327627877]

MaxPrnew=[241.08095445711552,
 1726.9903228626436,
 1232.262749719154,
 484.3360494454877,
 1232.262749719154]

k = [x for x,y in enumerate(zip(Pr_cap_t, MaxPrnew)) if y[0] > y[1]] # your program logic can be used in a list comprehension

false_indices = [[]]

false_indices.append(k)

false_indices
#[[], [0, 1, 2, 3, 4]]

huangapple
  • 本文由 发表于 2023年5月22日 16:10:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304185.html
匿名

发表评论

匿名网友

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

确定