3

Suppose I need a n elements long with each element seperated by a space. Further, I need the ith element to be a 1 and the rest to be a 0. What's the easiest pythonic way to do this?

Thus if n = 10, and i = 2, I would want 0 1 0 0 0 0 0 0 0 0

I want something like a new string[] that you can get in C++ but the Python version is eluding me.

Thanks!

1
  • Should the last character be a space or a 0/1? Commented May 8, 2012 at 22:57

5 Answers 5

6

Use a list.

n = 10
i = 2

mylist = ["0"] * n
mylist[i-1] = "1"
print " ".join(mylist)
Sign up to request clarification or add additional context in comments.

2 Comments

IMHO, it's more Pythonic to create the list with the correct elements in the first place, functional-style, rather than initialize and then modify the list, imperative-style. However, the imperative version does have the virtue of being quite clear, in this case.
Yeah, that was my goal. I love one-line list comprehensions normally.
2

The following Python generator expression should produce what you are looking for:

" ".join("1" if i == (x+1) else "0" for x in range(n))

2 Comments

" ".join("1" if x==1 else "0" for x in range(n))
@Morwenn, That works for the case where i=2. I think i is supposed to be a variable
1

The following example from the Python REPL should help you. The Python generator expression " ".join(bin((1<<n)+(1<<n>>i))[3:]) should be a solution.

>>> n=10
>>> i=2
>>> " ".join(bin((1<<n)+(1<<n>>i))[3:])
'0 1 0 0 0 0 0 0 0 0'

Comments

0

Just use "+" to join strings:

("0 "*i + "1 " + "0 "*(n-i-1))[:-1]

or you can also use bytearray:

a = bytearray(("0 "*n)[:-1])
a[i*2] = "1"
print str(a)

Comments

0

This looks like homework, so I'll describe my answer in English instead of Python.

Use a list comprehension to create a list of n strings, selecting each element from either '1' (if the index of the item is i) and '0' (otherwise). Join the elements of the list with a space between them.

There are several ways you can select from two items based on a boolean value. One of them would be to have the two items in a sequence, and index the sequence with the boolean value, thus picking the first element if False, and the second element if True. In this particular case, you can also convert the boolean value to '0' or '1' in several ways (e.g. convert to integer and then to string).

I don't understand the new string[] comment -- that's not C++ syntax.

Edit

Here's the Python:

' '.join([('0', '1')[x == i - 1] for x in range(n)])

or

' '.join([str(int(x == i - 1)) for x in range(n)])

I like the former more -- it's quite clear that it generates zeros and ones, and they can be changed to something else with ease. It's i - 1 instead of i to adjust for i being one-based and x being zero-based. Normally, I wouldn't do that. All things being equal, I prefer to work with zero-based indices throughout, except when input and output formats demand one-based values. In that case, I convert inputs as soon as possible and outputs as late as possible, and definitely avoid mixing the two in the same bit of code.

You can safely drop the square brackets, turning the list comprehension into a generator comprehension. For most intents and purposes, they work the same.

1 Comment

Don't worry, it's not hw. I need my protein mutation files to be in a specific format in order to run them through R. The 1 represents a mutation and the 0 represents no mutation for a specific amino acid in a specific cell line.

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.