For loop returning odd values












0















I have two lists I want to iterate through:



efw = [13.882352941176457, 10.854092526690406, 94.25675675675676, 17.851739788199694, 14.63844797178131, 8.166189111747846, 5.278592375366564, 3.4129692832764347, -6.413612565445015, 11.678832116788328, 23.859649122807003, 4.545454545454564, 10.105580693815996, -3.562340966921118, -0.6684491978609763, 2.285714285714291, 8.505747126436791]


and



gini = [3.9215686274509887, 6.190476190476191, -7.733812949640296, -16.608391608391603, -13.458262350936979, 7.505518763796926, -12.884615384615394, -20.21276595744681, -19.839679358717433, -10.885608856088568, -12.891986062717764, -15.56420233463035, -12.66540642722116, -12.802768166089962, -11.336032388663975, -13.507625272331147, -1.882845188284521]


I want to create two new lists with + and - depending on the values in gini and efw. If the value in gini is positive, then a + should be added to the g list. Same with the e and efw lists. I have tried:



g = 
e =
for n in gini:
if n > 0:
g.append("+")
g.append("-")

for f in efw:
if f > 0:
e.append("+")
e.append("-")


But for some positive values in gini there is a - sign... Why is the for loop appending the wrong symbols to the new lists?










share|improve this question


















  • 3





    didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

    – Eric Marcelino
    Nov 22 '18 at 2:28











  • Yes! Adding elif solved it!

    – Guillermina Sutter Schneider
    Nov 22 '18 at 2:30
















0















I have two lists I want to iterate through:



efw = [13.882352941176457, 10.854092526690406, 94.25675675675676, 17.851739788199694, 14.63844797178131, 8.166189111747846, 5.278592375366564, 3.4129692832764347, -6.413612565445015, 11.678832116788328, 23.859649122807003, 4.545454545454564, 10.105580693815996, -3.562340966921118, -0.6684491978609763, 2.285714285714291, 8.505747126436791]


and



gini = [3.9215686274509887, 6.190476190476191, -7.733812949640296, -16.608391608391603, -13.458262350936979, 7.505518763796926, -12.884615384615394, -20.21276595744681, -19.839679358717433, -10.885608856088568, -12.891986062717764, -15.56420233463035, -12.66540642722116, -12.802768166089962, -11.336032388663975, -13.507625272331147, -1.882845188284521]


I want to create two new lists with + and - depending on the values in gini and efw. If the value in gini is positive, then a + should be added to the g list. Same with the e and efw lists. I have tried:



g = 
e =
for n in gini:
if n > 0:
g.append("+")
g.append("-")

for f in efw:
if f > 0:
e.append("+")
e.append("-")


But for some positive values in gini there is a - sign... Why is the for loop appending the wrong symbols to the new lists?










share|improve this question


















  • 3





    didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

    – Eric Marcelino
    Nov 22 '18 at 2:28











  • Yes! Adding elif solved it!

    – Guillermina Sutter Schneider
    Nov 22 '18 at 2:30














0












0








0








I have two lists I want to iterate through:



efw = [13.882352941176457, 10.854092526690406, 94.25675675675676, 17.851739788199694, 14.63844797178131, 8.166189111747846, 5.278592375366564, 3.4129692832764347, -6.413612565445015, 11.678832116788328, 23.859649122807003, 4.545454545454564, 10.105580693815996, -3.562340966921118, -0.6684491978609763, 2.285714285714291, 8.505747126436791]


and



gini = [3.9215686274509887, 6.190476190476191, -7.733812949640296, -16.608391608391603, -13.458262350936979, 7.505518763796926, -12.884615384615394, -20.21276595744681, -19.839679358717433, -10.885608856088568, -12.891986062717764, -15.56420233463035, -12.66540642722116, -12.802768166089962, -11.336032388663975, -13.507625272331147, -1.882845188284521]


I want to create two new lists with + and - depending on the values in gini and efw. If the value in gini is positive, then a + should be added to the g list. Same with the e and efw lists. I have tried:



g = 
e =
for n in gini:
if n > 0:
g.append("+")
g.append("-")

for f in efw:
if f > 0:
e.append("+")
e.append("-")


But for some positive values in gini there is a - sign... Why is the for loop appending the wrong symbols to the new lists?










share|improve this question














I have two lists I want to iterate through:



efw = [13.882352941176457, 10.854092526690406, 94.25675675675676, 17.851739788199694, 14.63844797178131, 8.166189111747846, 5.278592375366564, 3.4129692832764347, -6.413612565445015, 11.678832116788328, 23.859649122807003, 4.545454545454564, 10.105580693815996, -3.562340966921118, -0.6684491978609763, 2.285714285714291, 8.505747126436791]


and



gini = [3.9215686274509887, 6.190476190476191, -7.733812949640296, -16.608391608391603, -13.458262350936979, 7.505518763796926, -12.884615384615394, -20.21276595744681, -19.839679358717433, -10.885608856088568, -12.891986062717764, -15.56420233463035, -12.66540642722116, -12.802768166089962, -11.336032388663975, -13.507625272331147, -1.882845188284521]


I want to create two new lists with + and - depending on the values in gini and efw. If the value in gini is positive, then a + should be added to the g list. Same with the e and efw lists. I have tried:



g = 
e =
for n in gini:
if n > 0:
g.append("+")
g.append("-")

for f in efw:
if f > 0:
e.append("+")
e.append("-")


But for some positive values in gini there is a - sign... Why is the for loop appending the wrong symbols to the new lists?







python list loops for-loop append






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:22









Guillermina Sutter SchneiderGuillermina Sutter Schneider

11511




11511








  • 3





    didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

    – Eric Marcelino
    Nov 22 '18 at 2:28











  • Yes! Adding elif solved it!

    – Guillermina Sutter Schneider
    Nov 22 '18 at 2:30














  • 3





    didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

    – Eric Marcelino
    Nov 22 '18 at 2:28











  • Yes! Adding elif solved it!

    – Guillermina Sutter Schneider
    Nov 22 '18 at 2:30








3




3





didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

– Eric Marcelino
Nov 22 '18 at 2:28





didn't you need to add else in your condition? every value in your array will appended with '-' with your current code

– Eric Marcelino
Nov 22 '18 at 2:28













Yes! Adding elif solved it!

– Guillermina Sutter Schneider
Nov 22 '18 at 2:30





Yes! Adding elif solved it!

– Guillermina Sutter Schneider
Nov 22 '18 at 2:30












1 Answer
1






active

oldest

votes


















2














Adding else solved it.



g = 
e =
for n in gini:
if n > 0:
g.append("+")
else:
g.append("-")


for f in efw:
if f > 0:
e.append("+")
else:
e.append("-")





share|improve this answer





















  • 2





    why use elif? you should just use else and get rid of the pass statements.

    – Xero Smith
    Nov 22 '18 at 2:33






  • 2





    using the continue statement can also simplify the code

    – Eric Marcelino
    Nov 22 '18 at 2:35






  • 2





    Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

    – Xero Smith
    Nov 22 '18 at 2:37











  • If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

    – svtag
    Nov 22 '18 at 12:48











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%2f53423028%2ffor-loop-returning-odd-values%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









2














Adding else solved it.



g = 
e =
for n in gini:
if n > 0:
g.append("+")
else:
g.append("-")


for f in efw:
if f > 0:
e.append("+")
else:
e.append("-")





share|improve this answer





















  • 2





    why use elif? you should just use else and get rid of the pass statements.

    – Xero Smith
    Nov 22 '18 at 2:33






  • 2





    using the continue statement can also simplify the code

    – Eric Marcelino
    Nov 22 '18 at 2:35






  • 2





    Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

    – Xero Smith
    Nov 22 '18 at 2:37











  • If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

    – svtag
    Nov 22 '18 at 12:48
















2














Adding else solved it.



g = 
e =
for n in gini:
if n > 0:
g.append("+")
else:
g.append("-")


for f in efw:
if f > 0:
e.append("+")
else:
e.append("-")





share|improve this answer





















  • 2





    why use elif? you should just use else and get rid of the pass statements.

    – Xero Smith
    Nov 22 '18 at 2:33






  • 2





    using the continue statement can also simplify the code

    – Eric Marcelino
    Nov 22 '18 at 2:35






  • 2





    Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

    – Xero Smith
    Nov 22 '18 at 2:37











  • If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

    – svtag
    Nov 22 '18 at 12:48














2












2








2







Adding else solved it.



g = 
e =
for n in gini:
if n > 0:
g.append("+")
else:
g.append("-")


for f in efw:
if f > 0:
e.append("+")
else:
e.append("-")





share|improve this answer















Adding else solved it.



g = 
e =
for n in gini:
if n > 0:
g.append("+")
else:
g.append("-")


for f in efw:
if f > 0:
e.append("+")
else:
e.append("-")






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 2:35

























answered Nov 22 '18 at 2:29









Guillermina Sutter SchneiderGuillermina Sutter Schneider

11511




11511








  • 2





    why use elif? you should just use else and get rid of the pass statements.

    – Xero Smith
    Nov 22 '18 at 2:33






  • 2





    using the continue statement can also simplify the code

    – Eric Marcelino
    Nov 22 '18 at 2:35






  • 2





    Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

    – Xero Smith
    Nov 22 '18 at 2:37











  • If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

    – svtag
    Nov 22 '18 at 12:48














  • 2





    why use elif? you should just use else and get rid of the pass statements.

    – Xero Smith
    Nov 22 '18 at 2:33






  • 2





    using the continue statement can also simplify the code

    – Eric Marcelino
    Nov 22 '18 at 2:35






  • 2





    Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

    – Xero Smith
    Nov 22 '18 at 2:37











  • If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

    – svtag
    Nov 22 '18 at 12:48








2




2





why use elif? you should just use else and get rid of the pass statements.

– Xero Smith
Nov 22 '18 at 2:33





why use elif? you should just use else and get rid of the pass statements.

– Xero Smith
Nov 22 '18 at 2:33




2




2





using the continue statement can also simplify the code

– Eric Marcelino
Nov 22 '18 at 2:35





using the continue statement can also simplify the code

– Eric Marcelino
Nov 22 '18 at 2:35




2




2





Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

– Xero Smith
Nov 22 '18 at 2:37





Better still you can do it with a list comprehension: ["+" if i>0 else "-" for i in gini]

– Xero Smith
Nov 22 '18 at 2:37













If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

– svtag
Nov 22 '18 at 12:48





If one of the elements is 0, your code will add '-' sign for it. Shouldn't you have a case for that?

– svtag
Nov 22 '18 at 12:48




















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%2f53423028%2ffor-loop-returning-odd-values%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith