打印嵌套列表中的某些元素
我有一个函数,它可以打印低值和高值之间的运行时间总计。我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
', '.join(movie[0] for movie in i)
,而不仅仅是print()
语句中的i
。您需要迭代i
的元素。Try
', '.join(movie[0] for movie in i)
, rather than justi
in theprint()
statement. You need to iterate over the elements ofi
.