如何在Python中创建重复的元组列表? [英] How do you create a list of repeated tuples in Python?

查看:303
本文介绍了如何在Python中创建重复的元组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个元组列表,我从该列表中提取第一个元组,然后想创建一个仅包含第一个元组并重复n次以创建一个元组列表的新列表?谢谢.

Say I have a list of tuples, I extract the first tuple from this list and then want to create a new list that simply takes the first tuple and repeats it n times creating a list of tuples? Thanks.

推荐答案

由于元组是不可变的,因此您可以执行以下操作:

Since tuples are immutable you may be able to do:

lst = [tuples[0]]
repeated = lst * 10 # create a list of the first tuple repeated 10 times

如果它们包含可变对象,则由于它们是重复的相同对象,因此其中任何一个的更改都将反映在所有对象中.

If they contain mutable objects changes in any of them will reflect in all of them, since these are the same objects repeated.

这篇关于如何在Python中创建重复的元组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆