python列表变元组,Python:将列表列表转换为元组的元组

A Python newbie! I need help converting a list of lists tuples of tuples.

I want to call the append_as_tuples function, but every time I return it, it says

it can only concatenate lists (not tuples) to lists

Here is what I have so far:

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([])

解决方案

To convert list_of_lists to a tuple of tuples, use

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值