每天資訊python程式設計基礎(十一)

菜單

python程式設計基礎(十一)

python程式設計基礎(十一)

python程式設計基礎(十一)

python程式設計基礎(十一)

01:31

未知來源

python程式設計基礎(十一)

例3.對列表進行倒序處理,具體如下,將第四個元素以前的全部元素倒敘,對第四個之後的元素(包括第四個元素)也進行倒序,最後將整個列表倒序並輸出。

01

將要求倒序的兩部分元素分解到兩別表進行倒序後,拼接並整體倒序即可。

02

在1的基礎上,分解方法可以是迴圈遍歷並新增元素到新列表,其中遍歷部長為-1可實現倒序,接著拼接,整體倒序輸出。

03

在1.2的基礎上,用切片代替迴圈遍歷,也可實現直接倒序。

04

理解其實質。將一串數字分為兩部分,第一部分現在原位置進行倒序,在第二次倒序中,第一部分的整體到原兩部分的後一部分的位置且二次倒序,故最終的順序與原順序一致;第二部分同理。

故問題的實質為將前三個元素刪除並按原有順序新增到最後。

python程式設計基礎(十一)

python程式設計基礎(十一)

英文翻譯

Example 3. Reverse the order of the list. The specifics are as follows: flashback all the elements before the fourth element, reverse the elements after the fourth (including the fourth element), and finally reverse the entire list and output it.

Idea 1: Decompose the two parts of elements that require reverse order into two separate tables for reverse order, splicing and reverse order as a whole.

Idea 2: On the basis of 1, the decomposition method can be loop traversal and add elements to a new list, where the traversal length is -1 to achieve reverse order, then splicing, and the overall reverse order output.

Idea 3: On the basis of 1.2, using slices instead of loop traversal can also achieve direct reverse order.

Idea 4: Understand its essence. Divide a string of numbers into two parts. The first part is reversed from the original position. In the second reverse order, the whole of the first part is in the second part of the original two parts and the second part is reversed, so the final order is the same as the original order. ; The second part is the same.

Therefore, the essence of the question is to delete the first three elements and add them to the end in the original order.

參考資料:《python程式設計基礎》(董付國)

翻譯:Google翻譯

本文由LearningYard新學苑原創,部分圖片文字來自網路,如有侵權請聯絡。