简体   繁体   English

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

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

I have a list of bitstrings and for every bitstring there are 2 entangled bits that are always either 00 or 11. How do I find the indices of the bits assuming all of the strings are the same length?我有一个位串列表,对于每个位串,都有 2 个纠缠位,它们始终为 00 或 11。假设所有字符串的长度都相同,我如何找到这些位的索引?

For example lets say there are 3 bits and this is the list:例如,假设有 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

I tried mapping to find which are always 00 but this doesn't work because they can also be 11 for larger strings.我尝试映射以查找始终为 00 的映射,但这不起作用,因为对于较大的字符串,它们也可以为 11。 thanks谢谢

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')

Considering that in a string 3 chars long, the most common value must be the 'entangled' one.考虑到在 3 个字符长的字符串中,最常见的值必须是“纠缠”的值。 So we can just map each list to whether or not the individual bits equal the most common bit, then look for the columns where the results are all the same.所以我们可以只 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 Output

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

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

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