Function returning same variable separated by a comma












0















I don't understand the point of this function returning two variables, which are the same:



def construct_shingles(doc,k,h):
#print 'antes -> ',doc,len(doc)
doc = doc.lower()
doc = ''.join(doc.split(' '))
#print 'depois -> ',doc,len(doc)
shingles = {}
for i in xrange(len(doc)):
substr = ''.join(doc[i:i+k])
if len(substr) == k and substr not in shingles:
shingles[substr] = 1

if not h:
return doc,shingles.keys()

ret = tuple(shingles_hashed(shingles))

return ret,ret


Seems redundant, but there must be a good reason for it, I just don't see why. Perhaps because there are two return statements? If 'h' is true, does it return both return statements? The calling functions look like:



def construct_set_shingles(docs,k,h=False):
shingles =
for i in xrange(len(docs)):
doc = docs[i]
doc,sh = construct_shingles(doc,k,h)
docs[i] = doc
shingles.append(sh)
return docs,shingles


and



def shingles_hashed(shingles):
global len_buckets
global hash_table
shingles_hashed =
for substr in shingles:
key = hash(substr)
shingles_hashed.append(key)
hash_table[key].append(substr)
return shingles_hashed


The data set and function call look like:



k = 3 #number of shingles

d0 = "i know you"
d1 = "i think i met you"
d2 = "i did that"
d3 = "i did it"
d4 = "she says she knows you"
d5 = "know you personally"
d6 = "i think i know you"
d7 = "i know you personally"

docs = [d0,d1,d2,d3,d4,d5,d6,d7]
docsChange,shingles = construct_set_shingles(docs[:],k)


The github location: lsh/LHS










share|improve this question




















  • 1





    Not all you find on github will make sense.

    – Merlin
    Nov 16 '18 at 4:52
















0















I don't understand the point of this function returning two variables, which are the same:



def construct_shingles(doc,k,h):
#print 'antes -> ',doc,len(doc)
doc = doc.lower()
doc = ''.join(doc.split(' '))
#print 'depois -> ',doc,len(doc)
shingles = {}
for i in xrange(len(doc)):
substr = ''.join(doc[i:i+k])
if len(substr) == k and substr not in shingles:
shingles[substr] = 1

if not h:
return doc,shingles.keys()

ret = tuple(shingles_hashed(shingles))

return ret,ret


Seems redundant, but there must be a good reason for it, I just don't see why. Perhaps because there are two return statements? If 'h' is true, does it return both return statements? The calling functions look like:



def construct_set_shingles(docs,k,h=False):
shingles =
for i in xrange(len(docs)):
doc = docs[i]
doc,sh = construct_shingles(doc,k,h)
docs[i] = doc
shingles.append(sh)
return docs,shingles


and



def shingles_hashed(shingles):
global len_buckets
global hash_table
shingles_hashed =
for substr in shingles:
key = hash(substr)
shingles_hashed.append(key)
hash_table[key].append(substr)
return shingles_hashed


The data set and function call look like:



k = 3 #number of shingles

d0 = "i know you"
d1 = "i think i met you"
d2 = "i did that"
d3 = "i did it"
d4 = "she says she knows you"
d5 = "know you personally"
d6 = "i think i know you"
d7 = "i know you personally"

docs = [d0,d1,d2,d3,d4,d5,d6,d7]
docsChange,shingles = construct_set_shingles(docs[:],k)


The github location: lsh/LHS










share|improve this question




















  • 1





    Not all you find on github will make sense.

    – Merlin
    Nov 16 '18 at 4:52














0












0








0








I don't understand the point of this function returning two variables, which are the same:



def construct_shingles(doc,k,h):
#print 'antes -> ',doc,len(doc)
doc = doc.lower()
doc = ''.join(doc.split(' '))
#print 'depois -> ',doc,len(doc)
shingles = {}
for i in xrange(len(doc)):
substr = ''.join(doc[i:i+k])
if len(substr) == k and substr not in shingles:
shingles[substr] = 1

if not h:
return doc,shingles.keys()

ret = tuple(shingles_hashed(shingles))

return ret,ret


Seems redundant, but there must be a good reason for it, I just don't see why. Perhaps because there are two return statements? If 'h' is true, does it return both return statements? The calling functions look like:



def construct_set_shingles(docs,k,h=False):
shingles =
for i in xrange(len(docs)):
doc = docs[i]
doc,sh = construct_shingles(doc,k,h)
docs[i] = doc
shingles.append(sh)
return docs,shingles


and



def shingles_hashed(shingles):
global len_buckets
global hash_table
shingles_hashed =
for substr in shingles:
key = hash(substr)
shingles_hashed.append(key)
hash_table[key].append(substr)
return shingles_hashed


The data set and function call look like:



k = 3 #number of shingles

d0 = "i know you"
d1 = "i think i met you"
d2 = "i did that"
d3 = "i did it"
d4 = "she says she knows you"
d5 = "know you personally"
d6 = "i think i know you"
d7 = "i know you personally"

docs = [d0,d1,d2,d3,d4,d5,d6,d7]
docsChange,shingles = construct_set_shingles(docs[:],k)


The github location: lsh/LHS










share|improve this question
















I don't understand the point of this function returning two variables, which are the same:



def construct_shingles(doc,k,h):
#print 'antes -> ',doc,len(doc)
doc = doc.lower()
doc = ''.join(doc.split(' '))
#print 'depois -> ',doc,len(doc)
shingles = {}
for i in xrange(len(doc)):
substr = ''.join(doc[i:i+k])
if len(substr) == k and substr not in shingles:
shingles[substr] = 1

if not h:
return doc,shingles.keys()

ret = tuple(shingles_hashed(shingles))

return ret,ret


Seems redundant, but there must be a good reason for it, I just don't see why. Perhaps because there are two return statements? If 'h' is true, does it return both return statements? The calling functions look like:



def construct_set_shingles(docs,k,h=False):
shingles =
for i in xrange(len(docs)):
doc = docs[i]
doc,sh = construct_shingles(doc,k,h)
docs[i] = doc
shingles.append(sh)
return docs,shingles


and



def shingles_hashed(shingles):
global len_buckets
global hash_table
shingles_hashed =
for substr in shingles:
key = hash(substr)
shingles_hashed.append(key)
hash_table[key].append(substr)
return shingles_hashed


The data set and function call look like:



k = 3 #number of shingles

d0 = "i know you"
d1 = "i think i met you"
d2 = "i did that"
d3 = "i did it"
d4 = "she says she knows you"
d5 = "know you personally"
d6 = "i think i know you"
d7 = "i know you personally"

docs = [d0,d1,d2,d3,d4,d5,d6,d7]
docsChange,shingles = construct_set_shingles(docs[:],k)


The github location: lsh/LHS







python function return-value return-type lsh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:37







spacedustpi

















asked Nov 16 '18 at 4:43









spacedustpispacedustpi

1199




1199








  • 1





    Not all you find on github will make sense.

    – Merlin
    Nov 16 '18 at 4:52














  • 1





    Not all you find on github will make sense.

    – Merlin
    Nov 16 '18 at 4:52








1




1





Not all you find on github will make sense.

– Merlin
Nov 16 '18 at 4:52





Not all you find on github will make sense.

– Merlin
Nov 16 '18 at 4:52












1 Answer
1






active

oldest

votes


















1














Your guess is correct, and regarding why return ret,ret, the answer is that return statement is meant to return a pair of equalling values rather than one.



It is more of a style of coding rather than algorithm, because this can be done by other syntaxes. However this one is advantageous in some cases, e.g. if we write



def func(x, y, z):
...
return ret

a = func(x, y, z)
b = func(x, y, z)


then func would be executed twice. But if:



def func(x, y, z):
...
return ret, ret

a, b = func(x, y, z)


then func can be executed only once while being able to return to both a and b



Also in your particular case:



If h is false then the program until executes until the line return doc,shingles.keys(), and then the variables doc and sh in construct_set_shingles respectively take values of doc and shingles.keys().



Otherwise, the first return statement is omitted, the second one is executed and then both doc and sh take equal values, particularly equalling to the value of tuple(shingles_hashed(shingles))






share|improve this answer
























  • The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

    – spacedustpi
    Nov 16 '18 at 13:39













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331567%2ffunction-returning-same-variable-separated-by-a-comma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your guess is correct, and regarding why return ret,ret, the answer is that return statement is meant to return a pair of equalling values rather than one.



It is more of a style of coding rather than algorithm, because this can be done by other syntaxes. However this one is advantageous in some cases, e.g. if we write



def func(x, y, z):
...
return ret

a = func(x, y, z)
b = func(x, y, z)


then func would be executed twice. But if:



def func(x, y, z):
...
return ret, ret

a, b = func(x, y, z)


then func can be executed only once while being able to return to both a and b



Also in your particular case:



If h is false then the program until executes until the line return doc,shingles.keys(), and then the variables doc and sh in construct_set_shingles respectively take values of doc and shingles.keys().



Otherwise, the first return statement is omitted, the second one is executed and then both doc and sh take equal values, particularly equalling to the value of tuple(shingles_hashed(shingles))






share|improve this answer
























  • The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

    – spacedustpi
    Nov 16 '18 at 13:39


















1














Your guess is correct, and regarding why return ret,ret, the answer is that return statement is meant to return a pair of equalling values rather than one.



It is more of a style of coding rather than algorithm, because this can be done by other syntaxes. However this one is advantageous in some cases, e.g. if we write



def func(x, y, z):
...
return ret

a = func(x, y, z)
b = func(x, y, z)


then func would be executed twice. But if:



def func(x, y, z):
...
return ret, ret

a, b = func(x, y, z)


then func can be executed only once while being able to return to both a and b



Also in your particular case:



If h is false then the program until executes until the line return doc,shingles.keys(), and then the variables doc and sh in construct_set_shingles respectively take values of doc and shingles.keys().



Otherwise, the first return statement is omitted, the second one is executed and then both doc and sh take equal values, particularly equalling to the value of tuple(shingles_hashed(shingles))






share|improve this answer
























  • The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

    – spacedustpi
    Nov 16 '18 at 13:39
















1












1








1







Your guess is correct, and regarding why return ret,ret, the answer is that return statement is meant to return a pair of equalling values rather than one.



It is more of a style of coding rather than algorithm, because this can be done by other syntaxes. However this one is advantageous in some cases, e.g. if we write



def func(x, y, z):
...
return ret

a = func(x, y, z)
b = func(x, y, z)


then func would be executed twice. But if:



def func(x, y, z):
...
return ret, ret

a, b = func(x, y, z)


then func can be executed only once while being able to return to both a and b



Also in your particular case:



If h is false then the program until executes until the line return doc,shingles.keys(), and then the variables doc and sh in construct_set_shingles respectively take values of doc and shingles.keys().



Otherwise, the first return statement is omitted, the second one is executed and then both doc and sh take equal values, particularly equalling to the value of tuple(shingles_hashed(shingles))






share|improve this answer













Your guess is correct, and regarding why return ret,ret, the answer is that return statement is meant to return a pair of equalling values rather than one.



It is more of a style of coding rather than algorithm, because this can be done by other syntaxes. However this one is advantageous in some cases, e.g. if we write



def func(x, y, z):
...
return ret

a = func(x, y, z)
b = func(x, y, z)


then func would be executed twice. But if:



def func(x, y, z):
...
return ret, ret

a, b = func(x, y, z)


then func can be executed only once while being able to return to both a and b



Also in your particular case:



If h is false then the program until executes until the line return doc,shingles.keys(), and then the variables doc and sh in construct_set_shingles respectively take values of doc and shingles.keys().



Otherwise, the first return statement is omitted, the second one is executed and then both doc and sh take equal values, particularly equalling to the value of tuple(shingles_hashed(shingles))







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 4:58









Mr.KMr.K

847




847













  • The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

    – spacedustpi
    Nov 16 '18 at 13:39





















  • The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

    – spacedustpi
    Nov 16 '18 at 13:39



















The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

– spacedustpi
Nov 16 '18 at 13:39







The extra return statement threw me off. I thought it was possibly a mistake, as the code actually does throw an 'out of index' error, when I set 'h' to true. But I see now that it is definitely unrelated.

– spacedustpi
Nov 16 '18 at 13:39






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331567%2ffunction-returning-same-variable-separated-by-a-comma%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine