Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
The combination of these functions can be used to perform the task of a single shift in list. The last element is added to the rest of the list to achieve this task using slicing.
Các vòng quay theo chu kỳ đã được thảo luận trong các bài viết trước đó. Nhưng đôi khi, chúng tôi chỉ yêu cầu một nhiệm vụ cụ thể, một phần của xoay, tức là phần tử cuối cùng sang phần tử đầu tiên trong danh sách. Điều này có ứng dụng trong lập trình ngày trong một số tiện ích nhất định. Hãy để thảo luận về những cách nhất định mà điều này có thể đạt được.
Phương pháp số 1: Sử dụng Danh sách cắt và Nhà điều hành +
, sự kết hợp của các chức năng này có thể được sử dụng để thực hiện nhiệm vụ của một ca làm việc trong danh sách. Yếu tố cuối cùng được thêm vào phần còn lại của danh sách để đạt được nhiệm vụ này bằng cách sử dụng cắt lát.
test_list
=
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]0
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]1
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]2
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]3
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]2
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]5__12
>>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
7 >>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
8>>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
9 +
>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
1>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
2Output:
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
test_list
=
5>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
6>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
1
8>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
+
0>>> a = ["two","three","one"]
>>> a = [a[-1]] + a[:-1]
>>> a
['one', 'two', 'three']
6>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
1
3>>> a = ["two","three","one"]
>>> a = [a[-1]] + a[:-1]
>>> a
['one', 'two', 'three']
This functionality can also
be achieved using the inbuilt functions of python viz.
def shift_first_last[lst]:
x = lst.pop[0]
y = lst.pop[]
lst.insert[0, y]
lst.insert[len[lst], x]
return lst
nums = [1,2,3,4,5,6,7]
print["Original list:"]
print[nums]
print["Shift last element to first position and first element to last position of the said list:"]
print[shift_first_last[nums]]
chars = ['s','d','f','d','s','s','d','f']
print["\nOriginal list:"]
print[chars]
print["Shift last element to first position and first element to last position of the said list:"]
print[shift_first_last[chars]]
1. The pop function returns the last element and that is inserted at front using the insert function.Các vòng quay theo chu kỳ đã được thảo luận trong các bài viết trước đó. Nhưng đôi khi, chúng tôi chỉ yêu cầu một nhiệm vụ cụ thể, một phần của xoay, tức là phần tử cuối cùng sang phần tử đầu tiên trong danh sách. Điều này có ứng dụng trong lập trình ngày trong một số tiện ích nhất định. Hãy để thảo luận về những cách nhất định mà điều này có thể đạt được.
Phương pháp số 1: Sử dụng Danh sách cắt và Nhà điều hành +
, sự kết hợp của các chức năng này có thể được sử dụng để thực hiện nhiệm vụ của một ca làm việc trong danh sách. Yếu tố cuối cùng được thêm vào phần còn lại của danh sách để đạt được nhiệm vụ này bằng cách sử dụng cắt lát.
+
7+
8+
9
>>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
7 >>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
8>>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
9 +
>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
1>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
2Output:
The original list is : [1, 4, 5, 6, 7, 8, 9, 12] The list after shift is : [12, 1, 4, 5, 6, 7, 8, 9]
Vui vẻ
chèn
Cách tốt nhất chỉ là chỉ ra như:
>>> a = ["two","three","one"]
>>> a.insert[0,a.pop[]]
>>> a
['one', 'two', 'three']
nhạc pop
Nhưng, nếu tôi nên làm một cái gì đó nguyên bản, tôi sẽ làm điều này:
>>> a = ["two","three","one"]
>>> a = [a.pop[]] + a
>>> a
['one', 'two', 'three']
lát cắt
Và, tập thể dục cuối cùng, điều này:
>>> a = ["two","three","one"]
>>> a = [a[-1]] + a[:-1]
>>> a
['one', 'two', 'three']
Danh sách Python: Bài tập - 189 với giải pháp
Viết một chương trình Python để chuyển phần tử cuối cùng sang vị trí đầu tiên và phần tử đầu tiên sang vị trí cuối cùng trong một danh sách nhất định.
Giải pháp mẫu:
Mã Python:
def shift_first_last[lst]:
x = lst.pop[0]
y = lst.pop[]
lst.insert[0, y]
lst.insert[len[lst], x]
return lst
nums = [1,2,3,4,5,6,7]
print["Original list:"]
print[nums]
print["Shift last element to first position and first element to last position of the said list:"]
print[shift_first_last[nums]]
chars = ['s','d','f','d','s','s','d','f']
print["\nOriginal list:"]
print[chars]
print["Shift last element to first position and first element to last position of the said list:"]
print[shift_first_last[chars]]
Đầu ra mẫu:
Original list: [1, 2, 3, 4, 5, 6, 7] Shift last element to first position and first element to last position of the said list: [7, 2, 3, 4, 5, 6, 1] Original list: ['s', 'd', 'f', 'd', 's', 's', 'd', 'f'] Shift last element to first position and first element to last position of the said list: ['f', 'd', 'f', 'd', 's', 's', 'd', 's']
Trình bày bằng hình ảnh:
Flowchart:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.
Trước đây: Viết chương trình Python để sắp xếp một danh sách các bộ dữ liệu nhất định trên phần tử được chỉ định. Write a Python program to sort a given list of tuples on specified element.
Next: Write a Python program to find the specified number of largest products from two given list, multiplying an element from each list.