2014-02-14 94 views
-1

在JavaScript我知道的2數據結構文字:Python數據結構文字

一個)對象常量=> {},例如

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

B)陣列字面=> [],例如

[ 4, 7, 8 ] 

在JavaScript中,我喜歡這些文字,因爲它們緊湊易讀。

我已經閱讀了Python也有一些:

數組文本:

[] => [ 4, 5 ] 

字典文字:

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

元組文字:

() => (45, 69) 

那些是Python的文字媲美JavaScript文字?

是否還有更多的Python數據結構文字?

字典文字和字典(...)有區別嗎?

感謝很多提前

回答

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} < =>置([1,2,3])

是否有(字典字面和字典之間的任何差異... )?

沒有

+0

爲什麼Python文字不能與JavaScript文字相比? –

+0

爲什麼Python代碼不能在JavaScript中工作,反之亦然? – SethMMorton

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: ' " ''' """