-1

I'm not Python developer, but I need to translate a script from that language into C#. So far so good, but... What does this statement do? I tried to look for it in the internet, but nothing found.

self.bands = 5
self.ci = [0] * self.bands

Could any1 describe what happens here? Thx in advance!

Nickon
  • 9,652
  • 12
  • 64
  • 119

2 Answers2

2

It multiplies the list of single zero five times:

self.ci = [0, 0, 0, 0, 0]

Could be written as:

self.ci = []
for i in xrange(self.bands):
    self.ci.append(0)
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 1
    @Nickon There's an important gotcha you should be aware of when using this feature. The items in the resulting list are not *copies* of the original item in brackets. They're *references* to it. So if the item was mutable (say, a `list`) instead of your immutable `0`, you will get a list of five references to that *same* object. This is pretty much always not what you want. – Lauritz V. Thaulow Sep 13 '12 at 10:27
  • Thanks for the description of that issue:) – Nickon Sep 14 '12 at 08:18
1

It multiplies a list of the integer 0 by self.bands, which will create a list of length self.bands, containing a bunch of repeated 0s.

This is the idiomatic way of creating a repeated sequence in Python.

You can also use it for strings, which are sequences:

>>> print "!" * 10
!!!!!!!!!!
unwind
  • 391,730
  • 64
  • 469
  • 606