小编use*_*696的帖子

为什么我不能检测到元组是空的?

我在numpy中使用where函数来查找字符串数组中的单字母字符串.例如:我将寻找'U'['B' 'U' 'A' 'M' 'R' 'O']和获得的指标'U'.

letter = 'U'
row = ['B', 'U', 'A', 'M', 'R', 'O']
letter_found = np.where(row == letter)
Run Code Online (Sandbox Code Playgroud)

但是,当我在寻找字符串数组中不存在的字母时,我得到一个空元组,如下所示:

(array([], dtype=int64),)
Run Code Online (Sandbox Code Playgroud)

我需要能够检测到它何时找不到我在数组中寻找的字母.

我尝试过以下方法:

if not letter_found:
    print 'not found'
Run Code Online (Sandbox Code Playgroud)

但这不起作用.如何检测tuplewhere函数返回的numpy是空的?是因为我的一个变量可能是错误的类型?我是新手python和编程.

python arrays tuples numpy

6
推荐指数
1
解决办法
6515
查看次数

如何在列表中检查邻接,然后在python中修复邻接

我有这个清单:

row = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)

我需要随后洗牌或随机化列表:

shuffle(row)
Run Code Online (Sandbox Code Playgroud)

然后我需要通过找到任何相邻的1并移动它们,使它们至少相隔一个0.例如,我需要结果看起来像这样:

row = [0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0]
Run Code Online (Sandbox Code Playgroud)

我不知道最有效的方法是搜索相邻的1并然后移动它们以使它们不相邻...我将反复这样做以提出这一行的多个组合.

最初当列表更短时我就是这样做的:

row = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
rowlist = set(list(permutations(row)))
rowschemes = [(0, 0) + x for x in rowlist if '1, 1' not in …
Run Code Online (Sandbox Code Playgroud)

python algorithm list permutation

5
推荐指数
1
解决办法
342
查看次数

标签 统计

python ×2

algorithm ×1

arrays ×1

list ×1

numpy ×1

permutation ×1

tuples ×1