0

I'm trying to port some Python code to Java. I'm not familiar with Python and have never seen this in any language before:

return [c,] + s

What exactly does this line mean? Specifically the [c,] part. Is it combining two arrays or something? s is an array of integers and c is an integer. The full function is below (from Wikipedia: http://en.wikipedia.org/wiki/Ring_signature )

def sign(self,m,z):
    self.permut(m)
    s,u = [None]*self.n,random.randint(0,self.q)
    c = v = self.E(u) 
    for i in range(z+1,self.n)+range(z):
        s[i] = random.randint(0,self.q)
        v = self.E(v^self.g(s[i],self.k[i].e,self.k[i].n)) 
        if (i+1)%self.n == 0: c = v
    s[z] = self.g(v^u,self.k[z].d,self.k[z].n)
    return [c,] + s

Thanks so much!

tree-hacker
  • 5,351
  • 9
  • 38
  • 39

5 Answers5

5

The comma is redundant. It's just creating a one-element list:

>>> [1,]
[1]
>>> [1] == [1,]
True

The practice comes from creating tuples in Python; a one-element tuple requires a comma:

>>> (1)
1
>>> (1,)
(1,)
>>> (1) == (1,)
False

The [c,] + s statement creates a new list with the value of c as the first element.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

[c,] is exactly the same as [c], i.e. a single-item list.

(See this answer for why this syntax is needed)

Community
  • 1
  • 1
bereal
  • 32,519
  • 6
  • 58
  • 104
1

For a list, the extra comma is redundant and can be ignored. The only time it makes a difference if it had been a tuple instead of a list so

[c,] and [c] are the same but,
(c,) and (c) are different. The former being a tuple and later just a parenthesis around an expression
Abhijit
  • 62,056
  • 18
  • 131
  • 204
0

to answer both your questions, the line concatenates two lists, the first of the two is a one-element list since the comma is just ignored by python

furins
  • 4,979
  • 1
  • 39
  • 57
0

I believe you are correct, it is combining two "arrays" (lists in python). If I'm not mistaken, the trailing comma is unnecessary in this instance.

x = [1,2,3]
y = [1] + x

#is equivalent to

x = [1,2,3]
y = [1,] + x

The reason Python allows the use of trailing commas in lists has to do with another data type called a tuple and ease of use with multi-line list declaration in code.

Why does Python allow a trailing comma in list?

Community
  • 1
  • 1