Python - 元组,列表,字典及其之间转换

# 列表list 有序集合 索引操作 可变的序列


# 元组tuple 有序集合 索引操作 不可变的序列
    #     和list相比
    # 1.比列表操作速度快
    # 2.对数据“写保护“
    # 3.可用于字符串格式化中
    # 4.可作为字典的key


# 字典dict 无序集合 key操作 可变映射类型,哈希应用
    # 和list比较,dict有以下几个特点:
    # 1.查找和插入的速度极快,不会随着key的增加而增加
    # 2.需要占用大量的内存,内存浪费多
    # 而list相反:
    # 1.查找和插入的时间随着元素的增加而增加
    # 2.占用空间小,浪费内存很少

示例代码:

str1 = "czjmdnes"
test_1 = (1, 2, 3, 4, 5, 6)
test_2 = [1, 2, 3, 4, 5, 6, 7]
test_3 = {"name": "czj", "age":23, "height": 173}

print(type(str1))
print(type(test_1))
print(type(test_2))

# 元组 to 列表
test_1_tolist = list(test_1)
# 列表 to 元组
test_2_totuple = tuple(test_2)
# 字符串 to 列表
str1_tolist = list(str1)

print(type(test_1_tolist))
print(type(test_2_totuple))
print(type(str1_tolist))

运行结果:

<type 'str'>
<type 'tuple'>
<type 'list'>
<type 'list'>
<type 'tuple'>
<type 'list'>

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值