繁体   English   中英

在字符串列表 python 中查找匹配字符的索引

[英]find indices of matching characters in list of string python

我有一个位串列表,对于每个位串,都有 2 个纠缠位,它们始终为 00 或 11。假设所有字符串的长度都相同,我如何找到这些位的索引?

例如,假设有 3 位,这是列表:

list = ['000', '110', '001', '111']
# indices 0 and 1 are entangled because they are 
# always either 00 or 11 and index 2 is independent of them

我尝试映射以查找始终为 00 的映射,但这不起作用,因为对于较大的字符串,它们也可以为 11。 谢谢

for x in range(len(list[0])-1):
    if all(bits[x] == bits[x+1] for bits in list):
        print('index', x, 'and index', x+1, 'are entangled')

考虑到在 3 个字符长的字符串中,最常见的值必须是“纠缠”的值。 所以我们可以只 map 每个列表来判断各个位是否等于最常见的位,然后查找结果都相同的列。

import numpy as np
l =  ['000', '010', '101', '111']
l = np.array(l)


def true_false(e):
    return list(map(lambda x: x==str(np.bincount(list(e)).argmax()),list(e)))

b = np.array(list(map(true_false,l)))

np.all(b == b[0,:], axis = 0).nonzero()

Output

(array([0, 2], dtype=int64),)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM