Python:嵌套 For 循环以查找列表中元素的差异 --> 一个循环测试并附加到输出列表
Python: Nested For Loops To Find Difference In Elements In List --> One Loop Test & Appending to Output List
我彻底糊涂了!
我有一个列表如下:
prices = [13, 11, 10, 8, 5]
我想编写代码来反转列表以创建 [5, 8, 10, 11, 13]
并从 5 中减去每个项并将其附加到新列表。
所以我想要的输出是:[-3, -5, -6, -8]
我使用 break 编写了下面的代码来测试一个循环:
def max_profit(prices):
###
### YOUR CODE HERE
prices_reversed = list(reversed(prices))
print (f'prices_reversed is: {prices_reversed}')
profit_amt_list = []
for i in range(len(prices_reversed)):
print (f'i is: {i}')
current_value = prices_reversed[i]
for j in range(i,len(prices_reversed)):
print (j)
if j+1 < len(prices_reversed):
print ('true')
next_value = prices_reversed[j+1]
# print (current_value, next_value)
profit_amt_list.append(current_value - next_value)
break
print (profit_amt_list)
###
max_profit(prices)
我的代码打印:
prices_reversed is: [5, 8, 10, 11, 13]
i is: 0
0
true
1
true
2
true
3
true
4
**[-3, -5, -6, -8, -8]**
对于我的生活,我不明白为什么我有两个 -8 值,而列表应该只包含一个。请帮助我理解!
你把循环搞糊涂了。
首先,你的外循环是绝对没用的。您永远不会使用 i
的值,并且您 总是 在第一次迭代后中断。摆脱那个循环。
你的 j
循环的问题在于,虽然你试图找到一个简单的区别,但你把事情复杂化了太多以至于看不出问题所在。在最后一次迭代中,您没有停止(您有 5 个数字,因此只有 4 个差异),您只是懒得更新 next_value
,而是再次循环。由于 none 的临界值发生了变化,您再次执行 5-13 并将其添加到 profit_amt_list
.
因为current_value
总是5
,列表的第一个元素,直接计算:
profit_amt_list = []
price = prices_reversed[0]
for value in prices_reversed[1:]:
profit_amt_list.append(price - value)
print (profit_amt_list)
最终输出:
[-3, -5, -6, -8]
import numpy as np
arr = np.array([13, 11, 10, 8, 5])
arr = np.flip(arr)
arr = 5 - arr
print(arr[arr != 0])
输出
[-3, -5, -6, -8]
或者如果您希望它从上到下打印,则像这样。
for x in arr[arr != 0]:
print(x)
输出
-3
-5
-6
-8
我彻底糊涂了!
我有一个列表如下:
prices = [13, 11, 10, 8, 5]
我想编写代码来反转列表以创建 [5, 8, 10, 11, 13]
并从 5 中减去每个项并将其附加到新列表。
所以我想要的输出是:[-3, -5, -6, -8]
我使用 break 编写了下面的代码来测试一个循环:
def max_profit(prices):
###
### YOUR CODE HERE
prices_reversed = list(reversed(prices))
print (f'prices_reversed is: {prices_reversed}')
profit_amt_list = []
for i in range(len(prices_reversed)):
print (f'i is: {i}')
current_value = prices_reversed[i]
for j in range(i,len(prices_reversed)):
print (j)
if j+1 < len(prices_reversed):
print ('true')
next_value = prices_reversed[j+1]
# print (current_value, next_value)
profit_amt_list.append(current_value - next_value)
break
print (profit_amt_list)
###
max_profit(prices)
我的代码打印:
prices_reversed is: [5, 8, 10, 11, 13]
i is: 0
0
true
1
true
2
true
3
true
4
**[-3, -5, -6, -8, -8]**
对于我的生活,我不明白为什么我有两个 -8 值,而列表应该只包含一个。请帮助我理解!
你把循环搞糊涂了。
首先,你的外循环是绝对没用的。您永远不会使用 i
的值,并且您 总是 在第一次迭代后中断。摆脱那个循环。
你的 j
循环的问题在于,虽然你试图找到一个简单的区别,但你把事情复杂化了太多以至于看不出问题所在。在最后一次迭代中,您没有停止(您有 5 个数字,因此只有 4 个差异),您只是懒得更新 next_value
,而是再次循环。由于 none 的临界值发生了变化,您再次执行 5-13 并将其添加到 profit_amt_list
.
因为current_value
总是5
,列表的第一个元素,直接计算:
profit_amt_list = []
price = prices_reversed[0]
for value in prices_reversed[1:]:
profit_amt_list.append(price - value)
print (profit_amt_list)
最终输出:
[-3, -5, -6, -8]
import numpy as np
arr = np.array([13, 11, 10, 8, 5])
arr = np.flip(arr)
arr = 5 - arr
print(arr[arr != 0])
输出
[-3, -5, -6, -8]
或者如果您希望它从上到下打印,则像这样。
for x in arr[arr != 0]:
print(x)
输出
-3
-5
-6
-8