Khi mã hóa một danh sách rất dài trong Python, tốt hơn là phù hợp với một số mục trên mỗi dòng hay tôi nên giới hạn nó xuống còn 1 mỗi dòng? 99% thời gian tôi sẽ đi với Style2 bên dưới nhưng tôi có 5 danh sách mỗi danh sách có cùng chiều dài với phần dưới đây và có vẻ như có quá nhiều dòng. Lời khuyên nào sẽ được đánh giá cao.
Ví dụ:
style1 = [
'Email:', 'SSN:', 'Address:', 'Home Phone:',
'Mobile Phone: ', 'DOB:', 'Date of Surgery:',
'Date of Service:', 'Facility of Service:', 'Clinic Number:',
'Employer:', 'Work Phone: ', 'Fax: ', 'Type:', 'IPA:',
'Health Plan:', 'ID #:', 'Claims Address:', 'Group #:',
'Claim # / PO #:', 'Phone:', 'Fax:', 'Contact',
'Adjuster Email', 'Util Review Phone', 'Util Review Fax',
'Doctor:', 'NPI #: ', 'Date of Injury: ', 'Body Parts:',
'Body Part Side:', 'Gender:', 'Diagnosis:', 'Diagnosis 2:',
'Procedure:'
]
style2 = [
'Email:',
'SSN:',
'Address:',
'Home Phone:',
'Mobile Phone: ',
'DOB:',
'Date of Surgery:',
'Date of Service:',
'Facility of Service:',
'Clinic Number:',
'Employer:',
'Work Phone: ',
'Fax: ',
'Type:',
'IPA:',
'Health Plan:',
'ID #:',
'Claims Address:',
'Group #:',
'Claim # / PO #:',
'Phone:',
'Fax:',
'Contact',
'Adjuster Email',
'Util Review Phone',
'Util Review Fax',
'Doctor:',
'NPI #: ',
'Date of Injury: ',
'Body Parts:',
'Body Part Side:',
'Gender:',
'Diagnosis:',
'Diagnosis 2:',
'Procedure:'
]
Tôi sử dụng #3. Tương tự đối với danh sách dài, bộ dữ liệu, v.v ... Nó không yêu cầu thêm bất kỳ không gian bổ sung nào ngoài các vết lõm. Như mọi khi, hãy nhất quán.
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
Tương tự, đây là cách ưa thích của tôi bao gồm các chuỗi lớn mà không giới thiệu bất kỳ khoảng trắng nào [như bạn sẽ nhận được nếu bạn sử dụng chuỗi đa dòng được trích xuất ba]:
data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
Trong bài viết này, chúng tôi sẽ hiểu khái niệm về các tuyên bố đa dòng trong ngôn ngữ lập trình Python.
Tuyên bố trong Python:
Trong Python, một tuyên bố là một lệnh hợp lý mà một thông dịch viên Python có thể đọc và thực hiện. Nó có thể là một tuyên bố gán hoặc một biểu thức trong Python. & Nbsp;
Tuyên bố đa dòng trong Python:
Trong Python, các câu lệnh thường được viết trong một dòng duy nhất và ký tự cuối cùng của các dòng này là Newline. Để mở rộng câu lệnh sang một hoặc nhiều dòng, chúng ta có thể sử dụng niềng răng {}, dấu ngoặc đơn [], vuông [], bán đại tá, và ký tự tiếp tục chém \ \. Chúng tôi có thể sử dụng bất kỳ trong số này theo yêu cầu của chúng tôi trong mã. Với ký tự tiếp tục dòng, chúng ta có thể phân chia rõ ràng một câu lệnh dài thành nhiều dòng [\]. & NBSP;
Code:
Python3
g
=
"geeks\
for
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
0mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
1mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
2mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
3Trong mã trên nếu chúng ta không sử dụng các ký tự tiếp tục, mã sẽ đưa ra lỗi theo nghĩa đen của chuỗi chưa kết thúc.
Output:
geeksforgeeks
Tiếp tục dòng được chia thành hai cách khác nhau:
- Tiếp tục dòng rõ ràng
- Tiếp tục dòng ẩn
Tiếp tục dòng rõ ràng:
Trong loại câu lệnh đa dòng này, chúng tôi sẽ sử dụng ký tự tiếp tục dòng [\] để chia một câu lệnh thành nhiều dòng.
Example:
Trong ví dụ này, chúng tôi đang khởi tạo văn bản và biểu thức toán học bằng cách sử dụng dấu hiệu ‘\ \, đó là sự tiếp tục dòng rõ ràng để tiếp tục cùng một dòng trong nhiều dòng trong lập trình Python.
Python3
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
4=
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
6for
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
8mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
9data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
0data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
1 data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
2mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
9data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
4data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
1 data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
6data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
7data = [
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABG"
"l0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEN"
"xBRpFYmctaKCfwrBSCrRLuL3iEW6+EEUG8XvIVjYWNgJdhFjIX"
"rz6pKtPB5e5rmq7tmxk+hqO34e1or0yXTGrj9sXGs1Ib73efh2"
"AAAABJRU5ErkJggg=="
]
8mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
2geeksforgeeks0
geeksforgeeks1
geeksforgeeks2
geeksforgeeks3
geeksforgeeks4
geeksforgeeks5
=
geeksforgeeks7
geeksforgeeks8
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
0geeksforgeeks1
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 381
geeksforgeeks3
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
0geeksforgeeks1
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 385
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
2Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 387
geeksforgeeks1
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 389
geeksforgeeks3
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 381
Output:
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 38
Tiếp tục dòng tiềm ẩn:
Trong loại câu lệnh đa dòng này, tiếp tục dòng ẩn được sử dụng khi bạn phân chia một câu lệnh bằng cách sử dụng dấu ngoặc đơn [], dấu ngoặc [] và niềng răng {}. & Nbsp;
Example:
Trong ví dụ này, chúng tôi đang khởi tạo danh sách và biểu thức toán học bằng dấu ngoặc đơn [], dấu ngoặc [] và dấu hiệu {} dấu hiệu là tiếp tục dòng ẩn để tiếp tục cùng một dòng trong nhiều dòng trong lập trình Python.
Python3
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 382
=
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 384
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 3855____56
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 387
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 388
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 389
g
0Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 389
g
2Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 389
g
4Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 387
g
6mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
2g
8Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 382
=
0mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
9=
2geeksforgeeks3
=
4Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 382
=
6geeksforgeeks5
=
=
9geeksforgeeks7
geeksforgeeks8
"geeks\
2
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 381
geeksforgeeks3
"geeks\
2
Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 385
=
6mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
2Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 387
mydict = {
"key1": 1,
"key2": 2,
"key3": 3,
}
mylist = [
[1, 'hello'],
[2, 'world'],
]
nested = {
a: [
[1, 'a'],
[2, 'b'],
],
b: [
[3, 'c'],
[4, 'd'],
],
}
9Initializing a text using the Explicit multi-line statement A Computer Science portalfor geeks. It contains well written, well thought and well explained computer science and programming articles Initializing a mathematical expression using the Explicit multi-line statement 389
geeksforgeeks3
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 381
Output:
Initializing a list using the Implicit multi-line statement [5, 4, 3, 2, 1] Initializing a mathematical expression using the Explicit multi-line statement 38