Python:将列表列表转换为元组的元组

标签 python list function tuples

Python 新手!我需要帮助转换列表元组的列表。

我想调用 append_as_tuples 函数,但每次返回时,它都说
它只能将列表(不是元组)连接到列表

这是我目前所拥有的:

def append_as_tuple(t, l):
    ''' Convert list l to a tuple and append it to tuple t as a single value '''
    return t[:] + (tuple(l),)  

def convert_lists(lol):
    t = []
    if type(lol) == a or type(lol) == tuple:
        return [convert_lists(lol) for i in t]
    return append_as_tuples(lol,a)

#- test harness#

a=[range(5), range(10,20), ['hello', 'goodbye']]  
print a  
print convert_lists(a)  
print convert_lists([])  

最佳答案

要将 list_of_lists 转换为元组的元组,请使用

tuple_of_tuples = tuple(tuple(x) for x in list_of_lists)

关于Python:将列表列表转换为元组的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5506511/

相关文章:

r - mapply 和两个列表

计算 posix read() 接收到的字节数

python - 为什么这个 C++ 代码只比 Python 快一点点?

python - Flask 表单数据在提交时重复

python - 内置函数和用户自定义函数的不同属性

javascript:switch 语句中的常用命令

python - 替换为 "if"以根据参数在函数中执行不同的操作

javascript - 如何正确处理切换 html5 视频

Java 8 - 获取列表元素的 'parent' 对象

python - 重新排列列表中的项目,使得没有两个相邻项目是相同的