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!

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

(See this answer for why this syntax is needed)

Comments

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

Comments

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

Comments

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?

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.