zoukankan      html  css  js  c++  java
  • Python 与 JavaScript 的语法不同之处(split、join)

    JavaScript

    var str1 = ‘helloworld’

    var arr1 = str1.split()    //  ['helloworld']

    var arr2 = str1.split('')    //  ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

    var str2 = arr2.join()    //  "h,e,l,l,o,w,o,r,l,d"

    var str3 = arr2.join('')    //  'helloworld'

    var str3 = ''.join(arr2)    //   VM1495:1 Uncaught TypeError:"".join is not a function

    Python 

    s1 = 'helloworld'

    l1 = s1.split()    // ['helloworld']

    l2 = s1.split('')    //  ['helloworld']

    l3 = list(s1)    //  ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]

    s2 = ''.join(l3)    // 'helloworld'

    s3 = l3.join('')    //  AttributeError: 'list' object has no attribute 'join'

    由上面的例子可以得出一个结论,使用字符串的split()方法,这个字符串的方法得出的结果,在JavaScript 和Python 中是一样的,都是讲整个字符串作为数组(列表)的唯一的一个元素,如果想让字符串分割后成为一个数组(列表),在JavaScript 中需在split() 的方法中加入一个空字符串的参数,即split(''),但是在Python 中,使用同样的加入空字符串参数的方式和不加入参数的方式一样。想要得到字符串分割成列表,可以直接使用list 函数进行强转。

  • 相关阅读:
    oracle登录错误(ORA-01033:ORACLE initialization or shutdown in progress
    mssql 判断sql语句的执行效率语句
    关于 knockout js 学习中的疑问 (1)
    JS 根据Url参数名称来获取对应的值 方法封装
    账户
    windows库
    CentOS的Qt3和Qt4问题
    c/c++调用dll
    CentOS 安装g++
    输入法不见了
  • 原文地址:https://www.cnblogs.com/yungiu/p/11436027.html
Copyright © 2011-2022 走看看