打印嵌套列表中的某些元素

发布于 2025-01-10 12:33:14 字数 1986 浏览 0 评论 0原文

我有一个函数,它可以打印低值和高值之间的运行时间总计。我创建了一个 powerset 函数并在我的新运行时函数中使用它。

在那些介于低和高之间的新集合中,我需要打印电影的名称。我在打印所有标题时遇到问题。我尝试过使用相同的 for 循环,但我无法弄清楚。所以,我保留了 i。

def show_marathon_options(movies, low, high):
    sets = power_set(movies)
    count = 1
    for i in sets:
        runtime = 0
        for j in i:
            runtime += j[1]
        if low <= runtime <= high:
            print(f'{count}. Total runtime for {i}: {runtime} mins.')
            count += 1

输入:

movie_20 = [['Back to the Future', 116], ['How to Train Your Dragon', 98], ['The Land Before Time', 69],
                ['The Lord of the Rings: The Return of the King', 201], ['Tangled', 100], ['Titanic', 194],
                ['The Godfather', 202], ['Schindler''s List', 195], ['The Green Mile', 189],
                ['Fanny and Alexander', 188], ['Avengers: Endgame', 181], ['The Wolf of Wall Street', 180],
                ['Casino', 178], ['Braveheart', 178], ['Interstellar', 169], ['Django Unchained', 165],
                ['Saving Private Ryan', 169], ['Logan', 137], ['Tarzan', 88], ['Mulan', 88]]

输出:

1. Total runtime for [['Back to the Future', 116], ['How to Train Your Dragon', 98]]: 214 mins.
2. Total runtime for [['Back to the Future', 116], ['The Land Before Time', 69]]: 185 mins.
3. Total runtime for [['Back to the Future', 116], ['How to Train Your Dragon', 98], ['The Land Before Time', 69]]: 283 mins.
4. Total runtime for [['The Lord of the Rings: The Return of the King', 201]]: 201 mins.
5......

需要格式输出:

1. Total runtime for Back to the Future, How to Train Your Dragon: 214 mins.
2. Total runtime for Back to the Future, The Land Before Time: 185 mins.
3. Total runtime for Back to the Future, How to Train Your Dragon, The Land Before Time: 283 mins.
4. Total runtime for The Lord of the Rings: The Return of the King': 201 mins.
5....

我尝试过 j[0],但它只访问每行的第一个标题。

I have a function where it prints runtime totals between a low and high value. I have created a powerset function and used that in my new runtime function.

Within those new sets that are between the low and high, I need to print the names of the movies. I'm having trouble printing all the titles. I've tried using the same for loop, but I couldn't figure it out. So, I've kept the i.

def show_marathon_options(movies, low, high):
    sets = power_set(movies)
    count = 1
    for i in sets:
        runtime = 0
        for j in i:
            runtime += j[1]
        if low <= runtime <= high:
            print(f'{count}. Total runtime for {i}: {runtime} mins.')
            count += 1

input:

movie_20 = [['Back to the Future', 116], ['How to Train Your Dragon', 98], ['The Land Before Time', 69],
                ['The Lord of the Rings: The Return of the King', 201], ['Tangled', 100], ['Titanic', 194],
                ['The Godfather', 202], ['Schindler''s List', 195], ['The Green Mile', 189],
                ['Fanny and Alexander', 188], ['Avengers: Endgame', 181], ['The Wolf of Wall Street', 180],
                ['Casino', 178], ['Braveheart', 178], ['Interstellar', 169], ['Django Unchained', 165],
                ['Saving Private Ryan', 169], ['Logan', 137], ['Tarzan', 88], ['Mulan', 88]]

output:

1. Total runtime for [['Back to the Future', 116], ['How to Train Your Dragon', 98]]: 214 mins.
2. Total runtime for [['Back to the Future', 116], ['The Land Before Time', 69]]: 185 mins.
3. Total runtime for [['Back to the Future', 116], ['How to Train Your Dragon', 98], ['The Land Before Time', 69]]: 283 mins.
4. Total runtime for [['The Lord of the Rings: The Return of the King', 201]]: 201 mins.
5......

Needed format output:

1. Total runtime for Back to the Future, How to Train Your Dragon: 214 mins.
2. Total runtime for Back to the Future, The Land Before Time: 185 mins.
3. Total runtime for Back to the Future, How to Train Your Dragon, The Land Before Time: 283 mins.
4. Total runtime for The Lord of the Rings: The Return of the King': 201 mins.
5....

I've tried j[0], but it only accesses the first title of every line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2025-01-17 12:33:14

尝试使用 ', '.join(movie[0] for movie in i),而不仅仅是 print() 语句中的 i。您需要迭代 i 的元素。

Try ', '.join(movie[0] for movie in i), rather than just i in the print() statement. You need to iterate over the elements of i.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文