簡體   English   中英

Python:基於索引生成字符串數組

[英]Python: generate a string array based on indexing

我有兩個數組定義為:

Winter   =   np.array( [ 9, 10, 11, 12 ] ) 
Summer   =   np.array( [ 4, 5, 6, 7, 8 ] )
Other    =   np.array( [ 1, 2, 3 ] )

我還有另一個月份數組:

GivenMonths = np.array([ 3, 4,  8,  8, 10,  7])

我想生成一個 SeasonIdentifier 為:

SeasonIdentifier = [ 'Other', 'Summer', 'Summer', 'Summer', 'Winter', 'Summer' ] 

即對於 GivenMonths 的每個元素,我想找到它是來自 Winter 還是 Summer 數組並將該信息存儲在 SeasonIdentifier 中。

編輯:根據下面的評論對我的原始問題進行了更改

這是你想要的嗎 ?

SeasonIdentifier =  ['Summer' if x in Summer else 'Winter' if x in Winter else 'Other' for x in GivenMonths]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM