インデックス作成とスライスを学習した後、リストと組み込みメソッドについてさらに学習し始めました。メソッドは
です。何も返しません
- 追加
- 挿入
- 削除
- 並べ替え
- 逆
- クリア
int を返します
- インデックス
- カウント
str を返します
- ポップ
配信リストの小さな変更の場合は、組み込み関数自体で十分です。ただし、リストに対してさらに多くの操作を実行したい場合は、for ループ、if ループが必要になります。
たとえば、文字列のみを変換する必要があるリスト ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 3] があります。大文字。ここでは、リストまたは文字列メソッドを直接使用することはできません。理想的なロジックは次のとおりです
- リストを 1 つずつ繰り返す必要があります
- 項目が文字列または他の型であることを確認してください
- 文字列メソッド upper() と append() を適宜使用してください
ここでは for ループ、if、else 条件が必要です
delivery_list= ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 3] upper_list = [] for item in delivery_list: if type(item) == str: upper_list.append(item.upper()) else: upper_list.append(item) print('Upper case list is:', upper_list) Output: Upper case list is: ['PENCIL', 'NOTEBOOK', 'MARKER', 'HIGHLIGHTER', 'GLUE STICK', 'ERASER', 'LAPTOP', 3]
与えられた課題は、そのような方法とロジックについてさらに練習することでした。利用可能なロジックやメソッドを見つけるのは興味深いものでした。
解決した詳細は
Tasks_list.py ####################################################################################### #1. Create a list of five delivery items and print the third item in the list. #eg: [“Notebook”, “Pencil”, “Eraser”, “Ruler”, “Marker”] ####################################################################################### delivery_list = ['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker'] print (f'\t1. Third item in the list{delivery_list} is: {delivery_list[2]}') ####################################################################################### # 2.A new delivery item “Glue Stick” needs to be added to the list. # Add it to the end of the list and print the updated list. ####################################################################################### delivery_list.append('Glue Stick') print('\t2. Updated delivery list is:', delivery_list) ####################################################################################### # 3. Insert “Highlighter” between the second and third items and print the updated list. ####################################################################################### delivery_list.insert(2, 'Highlighter') print('\t3. Updated list by inserting Highlighter b/w 2nd &3rd:', delivery_list) ####################################################################################### # 4. One delivery was canceled. Remove “Ruler” from the list and print the updated list. ####################################################################################### delivery_list.remove('Ruler') print('\t4. Updated list after removing Ruler is:', delivery_list) ####################################################################################### # 5. The delivery man needs to deliver only the first three items. # Print a sublist containing only these items. ####################################################################################### sublist =[] for item in delivery_list[:3]: #sublist =sublist.append(item) #This is incorrect as sublist.append() returns None. sublist.append(item) print('\t5. Sublist of first three elements using loop:', sublist) print('\t Sublist of first three elements using slicing:', delivery_list[:3]) ####################################################################################### # 6.The delivery man has finished his deliveries. # Convert all item names to uppercase using a list comprehension and print the new list. ####################################################################################### uppercase_list=[] for item in delivery_list: uppercase_list.append(item.upper()) print('\t6. Uppercase list of delivery items:', uppercase_list) uppercase_list_lc = [item.upper() for item in delivery_list] print('\t6. Uppercase list using list compre:', uppercase_list_lc) ####################################################################################### # 7. Check if “Marker” is still in the list and print a message indicating whether it is found. # 8. Print the number of delivery items in the list. ####################################################################################### is_found= delivery_list.count('Marker') if 'Marker' in delivery_list: print(f'\t7. Marker is found {is_found} times') else: print(f'\t7. Marker is not found {is_found}') print(f'\t8. Number of delivery item from {delivery_list} is: ', len(delivery_list)) ####################################################################################### # 9. Sort the list of items in alphabetical order and print the sorted list # 10. The delivery man decides to reverse the order of his deliveries. # Reverse the list and print it ####################################################################################### delivery_list.sort() print(f'\t9. Sorted delivery list is {delivery_list}') delivery_list.reverse() print(f'\t10. Reverse list of delivery list is {delivery_list}') ####################################################################################### # 11. Create a list where each item is a list containing a delivery item and its delivery time. # Print the first item and its time. ####################################################################################### delivery_list_time = [['Letter', '10:00'], ['Parcel', '10:15'], ['Magazine', '10:45'], ['Newspaper', '11:00']] print('\t11. First delivery item and time', delivery_list_time[0]) delivery_list = ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop'] delivery_time = ['11:00','11:20','11:40','12:00','12:30','13:00', '13:30'] #Paring of two list using zip paired_list=list(zip(delivery_list,delivery_time)) print('\tPaired list using zip:', paired_list[0:2]) #Combine corresponding item from multiple list using zip and for item_time_list = [[item,time] for item, time in zip(delivery_list, delivery_time)] print ('\tItem_time_list using list comprehension: ', item_time_list[0:1]) ####################################################################################### # 12. Count how many times “Ruler” appears in the list and print the count. # 13. Find the index of “Pencil” in the list and print it. # 14. Extend the list items with another list of new delivery items and print the updated list. # 15. Clear the list of all delivery items and print the list. # 16. Create a list with the item “Notebook” repeated three times and print the list. # 17. Using a nested list comprehension, create a list of lists where each sublist contains an item # and its length, then print the new list. # 18. Filter the list to include only items that contain the letter “e” and print the filtered list. # 19. Remove duplicate items from the list and print the list of unique items. ####################################################################################### is_found= delivery_list.count('Ruler') print('\t12. The count of Ruler in delivery list is:', is_found) index_pencil = delivery_list.index('Pencil') print(f'\t13. Index of Pencil in {delivery_list} is {index_pencil}') small_list = ['Ink',''] delivery_list.extend(small_list) print('\t14. Extend list by extend:', delivery_list) item_time_list.clear() print('\t15. Clearing the list using clear():', item_time_list) Notebook_list = ['Notebook']*3 print('\t16. Notebook list is:', Notebook_list) #Filter the list with e letter delivery_list new_delivery_list = [] for item in delivery_list: if 'e' in item: new_delivery_list.append(item) print ('\t18. Filtered list with items containing e:', new_delivery_list) new_list_compre = [item for item in delivery_list if 'e' in item] print ('\t18. Filtered list by list comprehension:', new_list_compre) #Remove duplicate items delivery_list.extend(['Ink', 'Marker']) print('\t ', delivery_list) for item in delivery_list: if delivery_list.count(item) > 1: delivery_list.remove(item) print('\t19. Duplicate remove list:',delivery_list) print('\t19. Duplicate remove list:',list(set(delivery_list))) ####################################################################################### # 17. Using a nested list comprehension, create a list of lists where each sublist contains an item # and its length, then print the new list. ####################################################################################### #without list comprehension nested_list = [] for item in delivery_list: nested_list.append([item, len(item)]) print('\t17. ', nested_list[-1:-6:-1]) #Using list comprehension printing nested list nested_list = [[item,len(item)] for item in delivery_list] print('\t17. Nested list with length:', nested_list[:5])
答え:
PS C:\Projects\PythonSuresh> python Tasks_list.py 1. Third item in the list['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker'] is: Eraser 2. Updated delivery list is: ['Notebook', 'Pencil', 'Eraser', 'Ruler', 'Marker', 'Glue Stick'] 3. Updated list by inserting Highlighter b/w 2nd &3rd: ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Ruler', 'Marker', 'Glue Stick'] 4. Updated list after removing Ruler is: ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick'] 5. Sublist of first three elements using loop: ['Notebook', 'Pencil', 'Highlighter'] Sublist of first three elements using slicing: ['Notebook', 'Pencil', 'Highlighter'] 6. Uppercase list of delivery items: ['NOTEBOOK', 'PENCIL', 'HIGHLIGHTER', 'ERASER', 'MARKER', 'GLUE STICK'] 6. Uppercase list using list compre: ['NOTEBOOK', 'PENCIL', 'HIGHLIGHTER', 'ERASER', 'MARKER', 'GLUE STICK'] 7. Marker is found 1 times 8. Number of delivery item from ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Marker', 'Glue Stick'] is: 6 9. Sorted delivery list is ['Eraser', 'Glue Stick', 'Highlighter', 'Marker', 'Notebook', 'Pencil'] 10. Reverse list of delivery list is ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser'] 11. First delivery item and time ['Letter', '10:00'] Paired list using zip: [('Pencil', '11:00'), ('Notebook', '11:20')] Item_time_list using list comprehension: [['Pencil', '11:00']] 12. The count of Ruler in delivery list is: 0 13. Index of Pencil in ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop'] is 0 14. Extend list by extend: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 'Ink', ''] 15. Clearing the list using clear(): [] 16. Notebook list is: ['Notebook', 'Notebook', 'Notebook'] 18. Filtered list with items containing e: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser'] 18. Filtered list by list comprehension: ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser'] ['Pencil', 'Notebook', 'Marker', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', 'Ink', '', 'Ink', 'Marker'] 19. Duplicate remove list: ['Pencil', 'Notebook', 'Highlighter', 'Glue Stick', 'Eraser', 'Laptop', '', 'Ink', 'Marker'] 19. Duplicate remove list: ['', 'Ink', 'Pencil', 'Notebook', 'Marker', 'Eraser', 'Laptop', 'Highlighter', 'Glue Stick'] 17. [['Marker', 6], ['Ink', 3], ['', 0], ['Laptop', 6], ['Eraser', 6]] 17. Nested list with length: [['Pencil', 6], ['Notebook', 8], ['Highlighter', 11], ['Glue Stick', 10], ['Eraser', 6]]
以上がPython - リストとタスクの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

Python 3.6のピクルスファイルのロードレポートエラー:modulenotFounderror:nomodulenamed ...

風光明媚なスポットコメント分析におけるJieba Wordセグメンテーションの問題を解決する方法は?風光明媚なスポットコメントと分析を行っているとき、私たちはしばしばJieba Wordセグメンテーションツールを使用してテキストを処理します...

正規表現を使用して、最初の閉じたタグと停止に一致する方法は? HTMLまたは他のマークアップ言語を扱う場合、しばしば正規表現が必要です...


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)
