检查列表元素<一个值,做点什么 - 似乎无法解决



我最近在一两年后回到python,似乎已经失去了所有的编码能力!

我正试图弄清楚如何检查列表,看看是否所有元素都大于x,如果超过一半小于一个值,如果一个小于一个数值,那么就对它做出反应。

我的代码是

score = [80, 35, 50, 70]
actions = [0, 10, 25]
def check(score, actions):
for i in range(len(score)):
if (score[i] <= 60 and score[i] > 45):
score[i] += actions[1]
if (score[i] <= 45 and score[i] > 30):
score[i] += actions[2]
check(score, actions)
print (score)

我知道这是基本的,我只是在寻求帮助!

我还想创建一个函数,用分数减去行动成本的平均值来奖励,但似乎无法实现?

这就是我上面的:

action_cost = [0, 10, 25]
def rewards(score, reward, action_cost):
reward == [statistics.mean(score) - action_cost[1]]

rewards(score, reward, action_cost)
print(reward)

任何帮助都会很棒!谢谢

我希望我的代码能帮助你:

import numpy as np
array = np.array([80, 35, 50, 70])
x = int(input('x: '))
value_1 = int(input('value 1: '))
value_2 = int(input('value 2: '))
values_greater_than_x = array[array > x]
values_lesser_than_value_1 = array[array < value_1]
values_lesser_than_value_2 = array[array < value_2]
if len(values_greater_than_x) == len(array):
print(f'All elements are greater then {x}')
else:
print(f'There are elements lesser then {x}')
if len(values_lesser_than_value_1) > len(array)/2:
print(f'More then a half are lesser then {value_1}')
else:
print(f'Less then a half are lesser then {value_1}')
if len(values_lesser_than_value_2) >= 1:
print(f'At least one is lesser then {value_2}')
else:
print(f'No value is lesser then {value_2}')

你唯一要做的就是根据你的条件编辑代码。

相关内容

最新更新