zoukankan      html  css  js  c++  java
  • 字符串、列表、元组 中文输出问题

    >>> tmp = ['中国','英国']
    >>> tmp = tmp[:1] + ['美国'] + tmp[1:]
    >>> tmp = tmp[:1] + ['德国'] + tmp[1:]
    >>> tmp
    ['中国', '德国', '美国', '英国']
    >>> tmp = ['中国','英国']
    >>> tmp = tmp[:1] + ['美国'] + tmp[1:]
    >>> tmp
    ['中国', '美国', '英国']
    >>> tmp = tmp[:1] + ['德国',] + tmp[1:]
    >>> tmp2 = ('中国','英国')
    >>> tmp2 = tmp2[:1] + ('美国') + tmp2[1:]
    Traceback (most recent call last):
    File "<pyshell#46>", line 1, in <module>
    tmp2 = tmp2[:1] + ('美国') + tmp2[1:]
    TypeError: can only concatenate tuple (not "str") to tuple #只能元组和元组连接(相加)
    >>> tmp2 = tmp2[:1] + ('美国,') + tmp2[1:]
    Traceback (most recent call last):
    File "<pyshell#47>", line 1, in <module>
    tmp2 = tmp2[:1] + ('美国,') + tmp2[1:]
    TypeError: can only concatenate tuple (not "str") to tuple
    >>> tmp2 = tmp2[:1] + ('美国',) + tmp2[1:]
    >>> tmp2
    ('中国', '美国', '英国')

    >>> s1 = ('美国')
    >>> s2 = ('美国,')
    >>> s3 = ('美国',)
    >>> type(s1)
    <class 'str'> #说明('美国')是一个字符串,而不是元组
    >>> type(s2)
    <class 'str'> #说明('美国,')是一个字符串,而不是元组
    >>> type(s3)
    <class 'tuple'> #说明('美国',)才是元组

  • 相关阅读:
    ES6变量的解构赋值
    ES6新增内容
    Rvalue references
    range-based for statement
    Space in Template Expression, nullptr, and auto
    Type Alias、noexcept、override、final
    Variadic Template
    =default =delete
    为什么不要特化函数模版?
    boost::noncopyable 的作用
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/7812045.html
Copyright © 2011-2022 走看看