python - 列表数组的映射和过滤功能

标签 python mapping

我想使用函数 process_slide_index(x)

映射一组列表,如下所示

tiles_index:

[(1, 1024, 0, 16, 0, 0), (1, 1024, 0, 16, 0, 1), (1, 1024, 0, 16, 0, 2), (1, 1024, 0, 16, 0, 3), (1, 1024, 0, 16, 0, 4), (1, 1024, 0, 16, 0, 5), (1, 1024, 0, 16, 0, 6),...]

方 block :

tiles = map(lambda x: process_slide_index(x), tiles_index)

map 功能:

def process_slide_index(tile_index):
    print("PROCESS SLIDE INDEX")
    slide_num, tile_size, overlap, zoom_level, col, row = tile_index
    slide = open_slide(slide_num)
    generator = create_tile_generator(slide, tile_size, overlap)
    tile = np.asarray(generator.get_tile(zoom_level, (col, row)))

    return (slide_num, tile)

我正在应用 map 函数,但我似乎没有进入我的 process_slide_index(tile_index) 函数。

我还想过滤一些返回 FalseTrue 函数的结果。但是我的功能又一次没有达到过滤功能。

filtered_tiles = filter(lambda x: keep_tile(x, tile_size, tissue_threshold), tiles)

我做错了什么?

问候

编辑 我到达检查点消息 PROCESS SLIDE INDEX 的唯一方法是在之后添加 list(map(print, tiles)) tiles 行。我用它来尝试调试,我的打印件开始出现。我现在很困惑。

最佳答案

您正在使用 python3,在 python2 mapfilter 中返回一个列表,但在 python3 它们返回一个对象,您必须使用该对象才能获取值:

>>> l = list(range(10))
>>> def foo(x):
...     print(x)
...     return x+1
... 
>>> map(foo, l)
<map object at 0x7f69728da828>

要使用此对象,您可以使用 list 例如。注意这次 print 是如何调用的:

>>> list(map(foo, l))
0
1
2
3
4
5
6
7
8
9
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

这个对象是惰性的,这意味着它们一个一个地产生值。在 for 循环中将它们用作迭代器时检查差异:

>>> for e in map(foo, l):
...     print(e)
... 
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10

使用 list 做同样的事情,但将每个获取的值存储在该列表中。

关于python - 列表数组的映射和过滤功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493058/

相关文章:

.net - 有什么方法可以提高 Automapper 的性能吗?

struct - Solidity:在映射中创建包含映射的结构时出现问题

python - 为什么我的正则表达式失败?

python - 处于分离模式的 docker 容器立即退出

c# - 如何在每个层次结构 (TPH) 映射的表中共享公共(public)列名

python - 推进 basemap 绘图

python - 使用 Bx 和 By 在 python 中剪切图像以进行前向和后向映射

python - 处理 ZeroDivisionError 的最佳方法?

python - 使用 SMTP 库通过 python 发送电子邮件

python - 矫正倾斜的矩形图片和菱形形状