在 Python 中将列表转换为元组 [英] Convert list to tuple in Python

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

问题描述

我正在尝试将列表转换为元组.

Google 上的大多数解决方案都提供以下代码:

l = [4,5,6]元组(l)

但是,当我运行它时,代码会导致错误消息:

<块引用>

TypeError: 'tuple' 对象不可调用

我该如何解决这个问题?

解决方案

它应该可以正常工作.不要使用 tuplelist 或其他特殊名称作为变量名.这可能是导致您出现问题的原因.

<预><代码>>>>l = [4,5,6]>>>元组(l)(4, 5, 6)>>>tuple = 'whoops' # 不要这样做>>>元组(l)类型错误:元组"对象不可调用

I'm trying to convert a list to a tuple.

Most solutions on Google offer the following code:

l = [4,5,6]
tuple(l)

However, the code results in an error message when I run it:

TypeError: 'tuple' object is not callable

How can I fix this problem?

解决方案

It should work fine. Don't use tuple, list or other special names as a variable name. It's probably what's causing your problem.

>>> l = [4,5,6]
>>> tuple(l)
(4, 5, 6)

>>> tuple = 'whoops'   # Don't do this
>>> tuple(l)
TypeError: 'tuple' object is not callable

这篇关于在 Python 中将列表转换为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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