gpt4 book ai didi

Python-更改列表中值的随机频率

转载 作者:行者123 更新时间:2023-12-01 03:34:41 25 4
gpt4 key购买 nike

我有一个包含两个列表的列表,我希望随机选择这两个列表中的一个值,然后将它们乘以 0.5

例如,我收到这样的列表:

[[-0.03680804604507722, 0.022112919584121357], [0.05806232738548797, -0.004015137642131433]]

最佳答案

听起来您想要做的是迭代列表列表,并在每个列表中随机选择一个索引,将该索引处的值乘以 0.5 并将其放回列表中。

import random
l = [[-0.03680804604507722, 0.022112919584121357], [0.05806232738548797, -0.004015137642131433]]

# for each sub list in the list
for sub_l in l:
# select a random integer between 0, and the number of elements in the sub list
rand_index = random.randrange(len(sub_l))

# and then multiply the value at that index by 0.5
# and store back in sub list
sub_l[rand_index] = sub_l[rand_index] * 0.5

关于Python-更改列表中值的随机频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com