python 列表、字典、元组、字符串之间的转换
来源:互联网 发布:房地产行业数据网站 编辑:程序博客网 时间:2024/05/21 15:40
python 中字符串、元组、字典、列表之间的转换
dictionary
$ pythonPython 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> dict = {'name':'eric', 'age':18, 'job':'writer'}>>> #dict --- str... print (type(str(dict))), str(dict)<class 'str'>(None, "{'age': 18, 'job': 'writer', 'name': 'eric'}")>>> #dict --- tuple... print (tuple(dict))('age', 'job', 'name')>>> #dict --- list... print (list(dict))['age', 'job', 'name']>>>
list: 不能转换成dictionary
$ pythonPython 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> lis = ['hey', 'are' ,'you' ,'ok']>>> #list --- str... print (str(lis))['hey', 'are', 'you', 'ok']>>> #list --- tuple... print (tuple(lis))('hey', 'are', 'you', 'ok')>>>
string
$ pythonPython 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> str = '123qwe'>>> #str --- tuple... print (tuple(str))('1', '2', '3', 'q', 'w', 'e')>>> #str --- list... print (list(str))['1', '2', '3', 'q', 'w', 'e']>>> #str 到dic转换 需要满足一定格式:>>> print (type(eval("{'name':'eric', 'age':18}"))),(eval("{'name':'eric', 'age':18}"))<class 'dict'>(None, {'name': 'eric', 'age': 18})>>>
tuple
$ python Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.>>> tup = (1, 2, 3, 'a', 's', 'd') >>> #tuple --- string just all the tuple's elements are string type>>> print (' '.join(tup))Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: sequence item 0: expected str instance, int found>>> tup = ('1', '2', '3', 'a', 's', 'd')>>> print (' '.join(tup))1 2 3 a s d>>> print (''.join(tup))123asd>>> #tuple --- list>>> tup = (1, 2, 3, 'a', 's', 'd')>>> print (list(tup))[1, 2, 3, 'a', 's', 'd']>>> #不可转换成字典
加密游戏
#!/usr/bin/python3 # -*- coding: utf-8 -*-"""# Author: EricRay# Created Time : 2017-11-29 10:35:31# File Name: censor.py# Description:将文本的特殊字符用'*' 代替"""def censor(text, word): num = 0 text_y = '' text_t = text.split(' ') for i in range(len(text_t)): if text_t[i] == word: text_t[i] = '*' num += 1 if num == len(text_t): return '' else : return ' '.join(list(text_t))text = (input('Please input text:'))word = (input('Please input word:'))print (censor(text, word))#结果展示$ python censor.py Please input text:you are so smart you youPlease input word:you* are so smart * *
阅读全文
0 0
- python 列表、字典、元组、字符串之间的转换
- python 字符串,元组, 列表,字典之间的转换
- python 字符串,列表,元组,字典相互转换
- python字典 列表 元组 字符串 转换 简单案例
- 【知识】Python中字符串、元组、列表、字典的互相转换
- python中数组,元组,字典和字符串之间的转换
- python中数组,元组,字典和字符串之间的转换
- 字符串列表元组之间的转换,字符串的方法
- python列表、元组、字典、字符串操作
- Python 列表、元组、字典、字符串
- Python 元组、列表、字典、字符串的操作
- python 字符串 元组 列表 字典 间的相互转化
- python 字符串与字典之间的转换
- 元组、列表、字典、字符串的遍历
- 字符串,列表,元组,字典的区别
- 转自博客园Ruthless:python中字符串/元组/列表/字典转换
- python的元组、列表、字典
- python的列表,元组,集合,字典
- 2-9·Linux基本权限
- Android C++中 sp<> wp<>知识
- PHP 获取各种固定时间的方法
- 使用 sqoop从MySQL增量导出数据到hive
- 第一章 统计学习方法概论
- python 列表、字典、元组、字符串之间的转换
- JS判断是否是微信页面,判断手机操作系统(ios或android)并跳转到不同下载页面
- 快速上手MQ之activeMQ(3)--订阅者模式
- Less 编写css,速度快多了!!!
- 内连接,外链接(左连接、右连接、全连接),交叉连接大总结
- PHPRAP v1.0.7版本发布,新增数据字典功能
- 测试
- 指针1
- 1031-1034