英文:
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]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论