1

With string abcdefg and list [1, 1, 0, 0, 1, 1, 0], what is a Pythonic way to return all characters in the string that match 1 (on) in the list?

The desired output will be:

['a', 'b', 'e', 'f']

Thanks!

Edit:

One more question, is it possible to group ab and ef so that the output will look something like this: ['ab', 'ef']? Basically the idea is to group characters that are separated by 0. If no 0 then it'll be ['abcdefg']. Thanks!

1
  • 1
    Answers are in multiple styles. I guess none of us are Dutch. "There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch." Commented Mar 2, 2013 at 16:25

4 Answers 4

4

You can use itertools.compress for this purpose

>>> from itertools import compress
>>> list(compress("abcdefg", [1, 1, 0, 0, 1, 1, 0]))
['a', 'b', 'e', 'f']

If you do not want to import any modules, you can also use

>>> [e for e, i in zip("abcdefg", [1, 1, 0, 0, 1, 1, 0]) if i]
['a', 'b', 'e', 'f']

Based on your latest requirement

>>> from itertools import groupby
>>> st = "abcdefghijklm"
>>> some_list = [1,1,0,0,0,1,1,1,0,0,0,1,0]
>>> it_l = iter(some_list)
>>> [''.join(v) for k, v in groupby(st, key = lambda e:next(it_l)) if k]
['ab', 'fgh', 'l']

or better

>>> [''.join(zip(*v)[-1]) for k, v in groupby(zip(some_list, st), key = itemgetter(0)) if k]
['ab', 'fgh', 'l']
Sign up to request clarification or add additional context in comments.

5 Comments

is it possible to separate ab and ef so that the output will look something like this: ['ab', 'ef']? thanks!
@Rock how is that separating? It looks like joining to me. It'd also help if you were more explicit - what would the output be for ['a', 'b', 'e', 'f', 'g'] for instance?
@JonClements: wording stuff. my intention is to group all characters that are separated by 0. in your example, the output'll be ['abcdefg'].
@Rock: See the update in the answer. Next time please be clear with your requirement, or ask a new question
@Abhijit: I know. Since it's based on the same question, I didn't want to start a new one. I'll be clear next time.
2
[x[0] for x in zip("abcdefg", [1, 1, 0, 0, 1, 1, 0]) if x[1]]

Comments

1

Three solutions:

s = 'abcdefg'
li = [1, 1, 0, 0, 1, 1, 0]

print [c for i,c in enumerate(s) if li[i]]

print [s[i] for i,b in enumerate(li) if b]

it = iter(li)
print [c for c in s if it.next()]

My prefered one is the one with iter() : no zip, no enumerate, no itertool

Comments

1

Regarding your update - you an use itertools.groupby to identify runs of consecutive values, and build a list from that - eg:

text = 'abcdefg'
bits = [1, 1, 0, 0, 1, 1, 0]

from itertools import groupby
print [''.join(el[0] for el in g) for k, g in groupby(zip(text, bits), lambda L: L[1]) if k]
# ['ab', 'ef']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.