【问题标题】:How to sum random.choices?如何求和 random.choices?
【发布时间】:2022-11-27 23:56:16
【问题描述】:

我有一个歌曲列表:

my_favorite_songs = [
    ['Waste a Moment', 3.03],
    ['New Salvation', 4.02],
    ['Staying\' Alive', 3.40],
    ['Out of Touch', 3.03],
    ['A Sorta Fairytale', 5.28],
    ['Easy', 4.15],
    ['Beautiful Day', 4.04],
    ['Nowhere to Run', 2.58],
    ['In This World', 4.02],

我的任务是找出 3 首随机歌曲的总和? 你能帮我吗?

我试过这个 res = sum(random.choices(time, k=3)),但有一个错误。

【问题讨论】:

  • 您随机选择的这个time 是什么?另外 - 不要只是告诉我们您有错误,请告诉我们什么错误是。

标签: python


【解决方案1】:

尝试

sum([x[1] for x in random.choices(my_favorite_songs, k=3)])

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 2020-05-02
    • 2021-01-17
    • 2019-08-21
    • 2020-10-10
    • 2021-11-18
    • 2021-08-24
    • 2019-04-12
    相关资源
    最近更新 更多