Python数据结构文字

时间:2014-02-14 04:32:20

标签: javascript python

在JavaScript中我知道2个数据结构文字:

a)Object literal => {},例如

{ "first": "peter", "last": "miller }

b)数组文字=> [],例如

[ 4, 7, 8 ]

在JavaScript中,我喜欢这些文字,因为它们紧凑且易于阅读。

我读过Python也有一些:

数组文字:

[] => [ 4, 5 ]

字典文字:

{ "first": "peter", "last": "miller" }

元组文字:

() => (45, 69)

这些Python文字是否与JavaScript文字相当?

是否有更多Python数据结构文字?

字典文字和字典(...)之间有什么区别吗?

提前多多感谢

2 个答案:

答案 0 :(得分:1)

问。这些Python文字是否与JavaScript文字相当?

   JS objects are comparable to Python dicts
   JS arrays are comparable to Python arrays

问。是否有更多Python数据结构文字?

   Yes.  
   Python has tuples:  (10, 20, 30)
   Python has sets:    {10, 20, 30}

问。字典文字和字典(...)之间有什么区别吗?

   If you mean, a difference between JS and Python, the answer is Yes.
   JS dictionaries (objects) must have a string as a key.
   Python can use other types as a key.
   JS object literals do not allow a trailing comma.

   If you mean, a difference between Python literals and the dict() constructor,
   the answer depends on how you use the dict contructor.
   dict(a=1, b=2)        # only allows keys to be valid identifiers that aren't keywords
   dict([('x y', 0), ('def', 1), (12, 'tw')])  # is more flexible
   {'x y': 0, 'def': 1, 12: 'tw'} # is flexible and equivalent

<强> P.S。

   The are other JS versus Python differences besides the containers.
   Python has "True".  JS has "true"
   Python has "None".  JS has "none"
   Python has four string quoting characters: ' " ''' """

答案 1 :(得分:0)

  

这些Python文字是否与JavaScript文字相当?

基本上是的。

JS:[1, 2, ,4] => [1, 2, undefined, 4]
Python:[1, 2, , 4] => invalid syntax

JS:{key: 'value'}
Python:{key: 'value'} => name 'key' is not defined

  

是否有更多Python数据结构文字?

设置:
{1,2,3}&lt; =&gt;设置([1,2,3])

  

字典文字和字典(...)之间有什么区别吗?

没有