跳到主要內容

{}大括弧在PYTHON的2種用法

Ref:
https://stackoverflow.com/questions/9197324/what-is-the-meaning-of-curly-braces


{}大括弧在PYTHON的2種用法:
"Curly Braces" are used in Python to define a dictionary. A dictionary is a data structure that maps one value to another - kind of like how an English dictionary maps a word to its definition.

Python:
1.
dict = {
    "a" : "Apple",
    "b" : "Banana",
}

2.
They are also used to format strings, instead of the old C style using %, like:

ds = ['a', 'b', 'c', 'd']
x = ['has_{} 1'.format(d) for d in ds]

print x

['has_a 1', 'has_b 1', 'has_c 1', 'has_d 1']

留言