Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnBy using list comprehension one can achieve this task with ease and in one line. We run a loop for specific indices in RHS and assign them to the required variables.
Python3
Chúng tôi thường đi qua nhiệm vụ nhận các giá trị chỉ mục nhất định và gán các biến ra khỏi chúng. Cách tiếp cận chung mà chúng tôi làm theo là trích xuất từng phần tử danh sách theo chỉ mục của nó và sau đó gán nó cho các biến. Cách tiếp cận này đòi hỏi nhiều dòng mã hơn. Hãy để thảo luận về những cách nhất định để thực hiện nhiệm vụ này theo cách nhỏ gọn để cải thiện khả năng đọc. & NBSP;
Phương pháp số 1: Sử dụng danh sách hiểu bằng cách sử dụng danh sách hiểu người ta có thể đạt được nhiệm vụ này một cách dễ dàng và trong một dòng. Chúng tôi chạy một vòng lặp cho các chỉ số cụ thể trong RHS và gán chúng cho các biến cần thiết. & Nbsp;
test_list
=
[
1
,
4
____7
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3010____7__12
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
=
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
test_list
0test_list
1The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38 ________ 40 ________ 23 & nbsp;
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
=
3The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
Output:
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3
=
5=
6
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
=
9The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33Method #2 : Using itemgetter[] itemgetter function can also be used to perform this particular task. This function accepts the index values and the container it is working on and assigns to the variables.
Python3
[
1=
6
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
[
5Chúng tôi thường đi qua nhiệm vụ nhận các giá trị chỉ mục nhất định và gán các biến ra khỏi chúng. Cách tiếp cận chung mà chúng tôi làm theo là trích xuất từng phần tử danh sách theo chỉ mục của nó và sau đó gán nó cho các biến. Cách tiếp cận này đòi hỏi nhiều dòng mã hơn. Hãy để thảo luận về những cách nhất định để thực hiện nhiệm vụ này theo cách nhỏ gọn để cải thiện khả năng đọc. & NBSP;
Phương pháp số 1: Sử dụng danh sách hiểu bằng cách sử dụng danh sách hiểu người ta có thể đạt được nhiệm vụ này một cách dễ dàng và trong một dòng. Chúng tôi chạy một vòng lặp cho các chỉ số cụ thể trong RHS và gán chúng cho các biến cần thiết. & Nbsp;
test_list
=
[
1
,
4
____7
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3010____7__12
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
=
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
test_list
0test_list
1The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38 ________ 40 ________ 23 & nbsp;
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
=
3The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
Output:
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3
=
5=
6
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
=
9The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33Method #3 : Using itertools.compress[] compress function accepts boolean values corresponding to each index as True if it has to be assigned to the variable and False it is not to be used in the variable assignment.
Python3
[
1=
6
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
[
5Chúng tôi thường đi qua nhiệm vụ nhận các giá trị chỉ mục nhất định và gán các biến ra khỏi chúng. Cách tiếp cận chung mà chúng tôi làm theo là trích xuất từng phần tử danh sách theo chỉ mục của nó và sau đó gán nó cho các biến. Cách tiếp cận này đòi hỏi nhiều dòng mã hơn. Hãy để thảo luận về những cách nhất định để thực hiện nhiệm vụ này theo cách nhỏ gọn để cải thiện khả năng đọc. & NBSP;
Phương pháp số 1: Sử dụng danh sách hiểu bằng cách sử dụng danh sách hiểu người ta có thể đạt được nhiệm vụ này một cách dễ dàng và trong một dòng. Chúng tôi chạy một vòng lặp cho các chỉ số cụ thể trong RHS và gán chúng cho các biến cần thiết. & Nbsp;
test_list
=
[
1
,
4
____7
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3010____7__12
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 30
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 31
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 32
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 35
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 36
=
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 39
test_list
0test_list
1The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 38 ________ 40 ________ 23 & nbsp;
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 34
=
3The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 33
Output:
The original list is : [1, 4, 5, 6, 7, 3] The variables are : 4 6 3