以下はPython 3でtxtデータファイルを行列に読み込む方法です。参考値が高く、皆様のお役に立てれば幸いです。一緒に見てみましょう
1. プログラム例:
''' 数据文件:2.txt内容:(以空格分开每个数据) 1 2 2.5 3 4 4 7 8 7 ''' from numpy import * A = zeros((3,3),dtype=float) #先创建一个 3x3的全零方阵A,并且数据的类型设置为float浮点型 f = open('2.txt') #打开数据文件文件 lines = f.readlines() #把全部数据文件读到一个列表lines中 A_row = 0 #表示矩阵的行,从0行开始 for line in lines: #把lines中的数据逐行读取出来 list = line.strip('\n').split(' ') #处理逐行数据:strip表示把头尾的'\n'去掉,split表示以空格来分割行数据,然后把处理后的行数据返回到list列表中 A[A_row:] = list[0:3] #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行 A_row+=1 #然后方阵A的下一行接着读 #print(line) print(A) #打印 方阵A里的数据 打印结果: [[ 1. 2. 2.5] [ 3. 4. 4. ] [ 7. 8. 7. ]]
2. 行列にデータを読み取るロジック:
は、たとえば、次のようになります。 to:
1 2 3
4 5 6
7 8 9
上記のコードを例として行列に読み込みます:
A_row =0の場合、A[A_row:]を実行します。 = list[0: 3] 後部行列 A は次のとおりです: 3
A_row = 1の場合、A[A_row:] = list[0:3]を実行すると、行列Aは次のようになります: | ||
2 | 3 | |
5 | 6 |
56
A_row = 2 の場合、A[A_row:] = list[0:3] を実行すると、結果の行列 A は次のようになります: | ||
2 | 3 | |
5 | 6 |
89
つまり、上記のコード: | for line in lines: #先把逐行数据取出来 list = line.strip('\n').split(' ') #再通过处理,放回到list列表中 A[A_row:] = list[0:3] #然后把list列表的数据放到矩阵中 A_row+=1 |
|
関連する推奨事項: | Python で TXT ファイルを読み取る方法 | |
Python がファイル名を読み取ってリストを生成する方法 |
以上がPython3でtxtデータファイルを行列に読み込む方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 中国語版
中国語版、とても使いやすい

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ホットトピック









