python 列表、字典、元组、字符串之间的转换

  • python 中字符串、元组、字典、列表之间的转换

  • dictionary

    $ python
    Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609] on linux
    Type "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

    $ python
    Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609] on linux
    Type "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

    $ python
    Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
    [GCC 5.4.0 20160609] on linux
    Type "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 linux
    Type "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 you
    Please input word:you
    * are so smart * *

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值