- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个列表列表(或者概念上更准确的二维数组):
list = [[1,1,0,0,0],
[1,1,2,0,0],
[0,2,2,2,0],
[0,0,0,2,0],
[0,0,0,1,0]]
我想识别具有相同值的不同区域并重写列表,以便每个区域都有唯一的值,如下所示:
list = [[1,1,2,2,2],
[1,1,3,2,2],
[0,3,3,3,2],
[0,0,0,3,2],
[0,0,0,4,2]]
我主要尝试编写循环的变体,解析每个值的数组并将相邻值设置为彼此相等(是的,我猜这是多余的),但确保左上角的 1 岛与右下角的 1 不起作用。我的尝试往好了说是参差不齐,往坏了说是不起作用。示例:
for x in list_length:
for y in sublist_length:
try:
if list[x][y] == list[x+1][y]:
list[x+1][y] = list[x][y]
except:
pass
或
predetermined_unique_value = 0
for x in list_length:
for y in sublist_length:
try:
if list[x][y] == list[x+1][y]:
list[x+1][y] = predetermined_unique_value
predetermined_unique_value += 1
except:
pass
以及要检查的方向(从当前点/点开始的上、下、左、右)的许多细微变化,通过运行循环来强制循环,直到所有点都被分配了新值,等等。
显然我在这里遗漏了一些东西。我怀疑答案实际上非常简单,但我似乎无法在 google 或 reddit 上找到任何内容,或者在这里找到其他答案(我可能只是奇怪地概念化它,所以寻找错误的东西)。
重申一下,如何解析该列表列表以根据相同的数据将值组织到相邻区域中,并重写它以确保这些区域都具有唯一值? (即只有一个 0 值区域,一个 1 值区域,等等)
我希望这些信息足以帮助您帮助我,但事实上,我不确定如何做到这一点,因为我做错了。请随时询问更多信息。
最佳答案
基于this answer您可以使用 scipy 库中的 ndimage 来完成此操作。我将您的数据应用于他的答案,这就是我得到的结果:
from scipy import ndimage
import numpy as np
data_tup = ((1,1,0,0,0),
(1,1,2,0,0),
(0,2,2,2,0),
(0,0,0,2,0),
(0,0,0,1,0))
data_list = [[1,1,0,0,0],
[1,1,2,0,0],
[0,2,2,2,0],
[0,0,0,2,0],
[0,0,0,1,0]]
def find_clusters(array):
clustered = np.empty_like(array)
unique_vals = np.unique(array)
cluster_count = 0
for val in unique_vals:
labelling, label_count = ndimage.label(array == val)
for k in range(1, label_count + 1):
clustered[labelling == k] = cluster_count
cluster_count += 1
return clustered, cluster_count
clusters, cluster_count = find_clusters(data_list)
clusters_tup, cluster_count_tup = find_clusters(data_tup)
print(" With list of lists, Found {} clusters:".format(cluster_count))
print(clusters, '\n')
print(" With tuples of tuple, Found {} clusters:".format(cluster_count_tup))
print(clusters_tup)
Output:
With list of lists, Found 5 clusters:
[[2 2 0 0 0]
[2 2 4 0 0]
[1 4 4 4 0]
[1 1 1 4 0]
[1 1 1 3 0]]
With tuples of tuple, Found 5 clusters:
[[2 2 0 0 0]
[2 2 4 0 0]
[1 4 4 4 0]
[1 1 1 4 0]
[1 1 1 3 0]]
两次输出都是列表的列表。如果您希望有不同的功能,则需要更改内部功能。
关于python - 如何重写一个列表列表,使 "islands"个值彼此唯一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71980880/
我习惯于使用 Apache 服务器,所以当启用 mod_rewrite 时,我可以创建一个 htaccess 文件并使用 URL 重写。 这是我的 htaccess 文件: RewriteEngine
我正在尝试编写一个 mixin 来修改输出的父选择器。这个想法是,在调用 mixin 的情况下,父选择器需要对其进行字符串替换。我有大部分工作,但我不知道如何吞下 & . .test { @inc
我有一个本地目录(上传)和一个 S3 桶设置。 当用户上传图片时,文件存储在本地目录:/uploads/member_id/image_name30 分钟后,系统将文件上传到 S3 使用相同的路径:s
我正在尝试使用以下内容重写代理页面的正文链接: sub_filter http://proxied.page.come http://local.page.com; sub_filte
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 1年前关闭。 Improve this questi
我尝试在我的 JSF 应用程序中使用“重写”(http://ocpsoft.org/rewrite/)。 一切都很好,我已经创建了规则: .addRule(Join.path("/profile/{p
我可以在 AEM 中大致看到两种 URL 重写方法: /etc/map/http(s)下的Sling映射(sling:Mapping) 使用链接重写器/TransformerFactory 重写 UR
我有一个 onclick 函数,我想将 anchor 添加到 href 值。我不想更改 URL,因为我需要该网站仍然可以为没有 javascript 的人/出于 SEO 目的而运行。所以这是我尝试使用
我必须在 UILabel 中显示货币和价格。在一个标签中,但使用不同的字体大小。现在看起来像这样: ...我这样做是重写drawTextInRect:,如下所示: - (void)drawTextIn
我正在尝试使用以下内容进行重定向: RewriteRule ^reviews/area/Santa-Barbara%2F$"/reviews/area/santa-barbara" [R=301,NC
我使用 FOSUserBundle 并且我想覆盖他的 registerAction Controller 。我阅读了与覆盖 FOSUserBundle Controller 相关的文档,但它不起作用。
我正在尝试让 URL 重写在我的网站上运行。这是我的 .htaccess 的内容: RewriteEngine On RewriteRule ^blog/?$ index.php?page=blog
好吧,这让我发疯了......我正在尝试像这样重写我的网址: Now: http://www.somedomain.com/Somepage.aspx http://www.somedomain.co
final方法不能在子类中重写。但凭借 Scala 的魔力,这似乎是可能的。 考虑以下示例: trait Test { final def doIt(s: String): String = s
我有一个类似下面的查询: Select ser.key From dbo.Enrlmt ser Where ser.wd >= @FromDate AND ser.wd ser.wd
我是 nginx 的新手,只是想做一些我认为应该很简单的事情。如果我这样做:- curl http://localhost:8008/12345678 我希望返回 index.html 页面。但是我得
我们的一位客户创建了一个二维码,其中 url 中包含一个空格。 我将如何编写处理此问题的 nginx 重定向? 在字符串中使用诸如“%20”之类的东西的几次尝试似乎会导致 nginx 出错或使 con
我正在尝试覆盖 appendChild 方法,以便我可以控制动态创建的元素并在插入页面之前根据需要修改它们。我尝试了这个示例代码,只是为了看看它是否可以完成: var f = Element.prot
我目前正在使用以下功能,当用户单击某处以确定是否隐藏下拉菜单(在 react 中)。一切正常,但当我单击正文时,它会记录以下内容。 我尝试重写它几次,但我找不到解决这个问题的方法。 Uncaught
我正在开发一个 Spring Integration/Boot 应用程序。我使用多文档 application.yml (src/main/resources/application.yml) 来设置
我是一名优秀的程序员,十分优秀!