Python array vs. js Array (list)

Source: Internet
Author: User

This article is to share with you the PythonArray andJSArrayRelated content, come together to look at it, hope to learn Python to help you. Createbasically the same Js varperson = [' Xiaohong ', ' Wangwei ', ' Liudehua ']; Python person=[' Xiaohong ', ' Wangwei ', ' Liudehua '] Accessing elementsbasically the same Js Person[0]//xiaohong Python >>> person[0] ' Xiaohong ' slicesThis is not the same,JswithSlicefunction,pythondo not use this same to complete the slice, in addition,JSThe inside is a comma,pythonThe syntax is a colon. One is the parentheses, the other is the brackets. Js Person.slice (0,2)//["Xiaohong", "Wangwei"] Python >>> Person[0:2] [' Xiaohong ', ' Wangwei '] Get element Indexa clearly forIndexOf, a name ofIndex, Dora Js Person. IndexOf (' Xiaohong ')//0 Python >>> person.index (' Xiaohong ') 0 add a new element to the end Jsfrom the namePush,pythonfrom the nameAppend Js Person.push (' Daniu ')//4 person//["Xiaohong", "Wangwei", "Liudehua", "Daniu"] Python >>> person.append (' daniu ') >>> person [' Xiaohong ', ' Wangwei ', ' Liudehua ', ' Daniu '] Delete Element JS PopDelete the last one,ShiftDelete the first one. pythonThe specified index is arbitrarily deleted, and there arePop, withoutShift Js Person.Pop() "Daniu" Person (3) ["Xiaohong", "Wangwei", "Liudehua"] Person.shift () "Xiaohong" Person (2) ["Wangwei", "Liudehua"] Python >>> person.pop () ' Daniu ' >>> person [' Wangwei ', ' Liudehua '] >>> del (person[0]) >>> person [' Wangwei ', ' Liudehua ', ' Daniu '] SortBasically, it's allSortMethod Js Person.Sort() (3) ["Daniu", "Liudehua", "Wangwei"] Python >>> Person.sort () >>> person [' Daniu ', ' Liudehua ', ' Wangwei '] reversalbasically the same Js Person.reverse () (3) ["Wangwei", "Liudehua", "Daniu"] Python >>> person.reverse () >>> person [' Wangwei ', ' Liudehua ', ' Daniu '] Connection Array Js Jswith Contact,pythonwithExtend vararr = [' A ', ' B ', ' C '] var added=arr.concat ([+]) Added//(6) ["A", "B", "C", 1, 2, 3] Python >>> arr = [' A ', ' B ', ' C ']>>> arr.extend ([+)] >>> arr [' A ', ' B ', ' C ', 1, 2, 3] Joinshould sayPythonThis is a little bit of writing Js vararr = [' A ', ' B ', ' C ', 1, 2, 3] Arr.Join('-') "a-b-c-1-2-3" Python >>> arr = [' A ', ' B ', ' C ']>>> '-'. Join (arr) ' A-b-c ' Meta-grouptuples are immutable,Jsno tuples, if necessary, what are some of the alternative methods? JsNo Python >>> (s=) >>> S.Pop() Traceback (most recentPager Last): File "<PYSHELL#46>", line 1, in S.Pop() Attributeerror: ' Tuple ' object hasNoAttribute ' Pop ' Dictionary JsNone but can be implemented with array workarounds Python >>> phonebook = {' Alice ': ' 2341 ', ' Beth ': ' 9102 ', ' ceil ': ' 3258 '}>>> phonebook {' Beth ': ' 9102 ', ' ceil ': ' 3258 ', ' Alice ': ' 2341 '} Summarycan seePythonin order to simplify the writing of a lot of things, such as the same slice,pythonis more thanJSless code, lots of words. Source: Pinterest

Python array vs. js Array (list)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.