2

I want to know is it possible to include logical operator OR in the list item. For example:

CHARS = ['X','Y','Z']

change this line of code to something like: (I know this is not a correct way)

Can anyone help me?

CHARS = ['X','Y','Z','X OR Y','Y OR Z','X OR Z']

Example code:

import numpy as np

seqs = ["XYZXYZ","YZYZYZ"]

CHARS = ['X','Y','Z']
CHARS_COUNT = len(CHARS)

maxlen = max(map(len, seqs))
res = np.zeros((len(seqs), CHARS_COUNT * maxlen), dtype=np.uint8)

for si, seq in enumerate(seqs):
    seqlen = len(seq)
    arr = np.chararray((seqlen,), buffer=seq)
    for ii, char in enumerate(CHARS):
        res[si][ii*seqlen:(ii+1)*seqlen][arr == char] = 1

print res

It scan through to detect X first if it is occurred then will be awarded 1 then detect Y and last Z.

Output:

[[1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1]
 [0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 1]]

Expected output after include logical OR:

[[1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 1 1 1 0 1 1 0 1]
 [0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1 0 1 0 1 0 1]]
Xiong89
  • 767
  • 2
  • 13
  • 24

1 Answers1

0

The example below is a bit contrived, but using itertools.combinations would be a way to generate combinations of size n for a given list. Combine this with str.join() and you'd be able to generate strings as exemplified in the first part of your question:

import itertools

CHARS = ['X','Y','Z']
allCombinations = [" OR ".join(x) for i in range(1,len(CHARS)) for x in itertools.combinations(CHARS, i)]

print repr(allCombinations)

Output:

['X', 'Y', 'Z', 'X OR Y', 'X OR Z', 'Y OR Z']