python資料垂直變橫的方法:使用zip()函數,類似矩陣轉置的過程,將豎狀資料變成橫狀資料;語法「zip([iterable, ...] )」。此函數將物件中對應的元素打包成一個元組,然後傳回由這些元組組成的列表。
python現在是非常火熱的語言,現在各行各業都有許多人在學習Python,但是在學習的過程中遇到的困難也不少。例如,下面我將說的,如何將資料由豎狀變成橫狀。
第一種方法:
#橫狀資料:
def func(*kargs): return kargs, //有“,” l = func(5,3,4,5,6) for i in l: print (i)
#輸出為:
(5, 3, 4, 5, 6)
想要將Python的資料由垂直改為橫狀需要你會用
##改為豎狀資料:#
def func(*kargs): return kargs //无“,” l = func(5,3,4,5,6) for i in l: print (i)輸出為:
5 3 4 5 6這是因為return kargs 回傳的是傳入的參數即元組(5, 3, 4, 5, 6) 所以遍歷輸出為豎狀, 而return kargs , 實際將傳入的參數包裝成元組返回形如(kargs, ) 形式,在此例中為((5, 3, 4, 5, 6), ) 因為遍歷對像中只存在一個元組對象,即(5, 3, 4, 5, 6) 所以實際上只執行了print((5, 3, 4, 5, 6))。
第二種方法:
#1、python3.2
# -*- coding: utf-8 -*- def vertical_print(s): lines=s.strip('。').split(',') lines.reverse() print('\n'.join([' '.join(w) for w in zip(*lines)])) vertical_print("白日依山尽,黄河入海流,欲穷千里目,更上一层楼。")
2、python 2.5
# -*- coding: utf-8 -*- def vertical_print(s): lines=s.strip('。'.decode('utf8')).split(','.decode('utf8')) lines.reverse() print '\n'.join([' '.join(w) for w in zip(*lines)]) s="白日依山尽,黄河入海流,欲穷千里目,更上一层楼。".decode('utf8') vertical_print(s)zip()的作用是把傳入的多個iterable結合在一起。 如list(zip("abc","def","ghi"))得到的是[('a', 'd', 'g'), ('b', 'e' , 'h'), ('c', 'f', 'i')]。在這裡,從zip(*lines)得到每行應該輸出的字元所組成的元組。
本質上這個程式是一個類似矩陣轉置的過程,核心就是zip()函數的呼叫。
學習python要用python的思路做練習啊。盡量不受C的思路限制才能寫好python。以上是python資料豎著怎麼變橫的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境