Python - 将字符串映射到布尔数组

时间:2017-03-10 13:17:59

标签: python string numpy boolean mapping

我有一个像这样的布尔数组

bool_arr = [True, True, False]

我想将两个字符串映射到每个布尔值

string_arr = ['r', 'r, 'k']

我如何使用numpy来映射?

3 个答案:

答案 0 :(得分:2)

7374 extra bytes at beginning or within zipfile
  (attempting to process anyway)
  inflating: META-INF/MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: TEST
Implementation-Version: 0.0.1
Archiver-Version: Plexus Archiver
Built-By: prasathm
Implementation-Vendor-Id: TEST
Spring-Boot-Version: 1.4.2.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.WarLauncher
Start-Class: com.mytest.Application
Spring-Boot-Classes: WEB-INF/classes/
Spring-Boot-Lib: WEB-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_25
Implementation-URL: http://projects.spring.io/spring-boot/TEST/

答案 1 :(得分:2)

使用索引的矢量化方法 -

bool_arr = np.array([True, True, False]) # Input boolean array
strings = np.array(['k','r']) # Input array of strings for mapping

out = np.take(strings, bool_arr)
out = np.take(strings, bool_arr.astype(int))
out = strings[bool_arr.astype(int)]

如果我们只需要在两个字符串之间进行选择,请使用np.where -

np.where(bool_arr, 'r','k')

答案 2 :(得分:2)

您可以使用<!--template bindings={ "ng-reflect-ng-for-of": null }-->方法:

<ul class="listboxtickets">
          <li class="selectlistticket" *ngFor="let tick of ticketlists">
            <div class="atickid"> {{tick.id}}  </div>
            <div class="atickname"> {{tick.summary}} </div>
            <div class="atickstat"> <img class="staraimg" src="assets/images/star_icon.png"/> </div>
          <div class="namelinet"> <img src="assets/images/text_bottomline.png"/> </div>
        </li>
        </ul>

给出:

numpy.vectorize