How to exchange the places of the items in a python list?











up vote
0
down vote

favorite












A text is divided in groups with seven letters. Each group is scrambled with the key 6015423 (letter with index 0 on index 6, letter with index 1 on index 0, letter with index 2 on index 1...).



Instead of the correct word "serpent" (tried it only with the first seven-letter group, the same problem occurs when %7 is left out) my code yields a false result beginning with index 4: serpsne.



What is the mistake?



list=['e','r','n','t','e','p','s']
clear=
for x in list:
if list.index(x)%7==0:
a=list[list.index(x)+6]
elif list.index(x)%7==1:
a=list[list.index(x)-1]
elif list.index(x)%7==2:
a=list[list.index(x)-1]
elif list.index(x)%7==3:
a=list[list.index(x)+2]
elif list.index(x)%7==4:
a=x
elif list.index(x)%7==5:
a=list[list.index(x)-3]
else:
a=list[list.index(x)-6]
clear.append(a)
clear=''.join(clear)
print(clear)


(Don´t know why this box inserts two blank lines after for and else, my code has no blank lines.)










share|improve this question
























  • You could swap the values by index. list[0],list[1] = list[1],list[0]
    – van der Zon Stef
    Nov 22 at 8:36

















up vote
0
down vote

favorite












A text is divided in groups with seven letters. Each group is scrambled with the key 6015423 (letter with index 0 on index 6, letter with index 1 on index 0, letter with index 2 on index 1...).



Instead of the correct word "serpent" (tried it only with the first seven-letter group, the same problem occurs when %7 is left out) my code yields a false result beginning with index 4: serpsne.



What is the mistake?



list=['e','r','n','t','e','p','s']
clear=
for x in list:
if list.index(x)%7==0:
a=list[list.index(x)+6]
elif list.index(x)%7==1:
a=list[list.index(x)-1]
elif list.index(x)%7==2:
a=list[list.index(x)-1]
elif list.index(x)%7==3:
a=list[list.index(x)+2]
elif list.index(x)%7==4:
a=x
elif list.index(x)%7==5:
a=list[list.index(x)-3]
else:
a=list[list.index(x)-6]
clear.append(a)
clear=''.join(clear)
print(clear)


(Don´t know why this box inserts two blank lines after for and else, my code has no blank lines.)










share|improve this question
























  • You could swap the values by index. list[0],list[1] = list[1],list[0]
    – van der Zon Stef
    Nov 22 at 8:36















up vote
0
down vote

favorite









up vote
0
down vote

favorite











A text is divided in groups with seven letters. Each group is scrambled with the key 6015423 (letter with index 0 on index 6, letter with index 1 on index 0, letter with index 2 on index 1...).



Instead of the correct word "serpent" (tried it only with the first seven-letter group, the same problem occurs when %7 is left out) my code yields a false result beginning with index 4: serpsne.



What is the mistake?



list=['e','r','n','t','e','p','s']
clear=
for x in list:
if list.index(x)%7==0:
a=list[list.index(x)+6]
elif list.index(x)%7==1:
a=list[list.index(x)-1]
elif list.index(x)%7==2:
a=list[list.index(x)-1]
elif list.index(x)%7==3:
a=list[list.index(x)+2]
elif list.index(x)%7==4:
a=x
elif list.index(x)%7==5:
a=list[list.index(x)-3]
else:
a=list[list.index(x)-6]
clear.append(a)
clear=''.join(clear)
print(clear)


(Don´t know why this box inserts two blank lines after for and else, my code has no blank lines.)










share|improve this question















A text is divided in groups with seven letters. Each group is scrambled with the key 6015423 (letter with index 0 on index 6, letter with index 1 on index 0, letter with index 2 on index 1...).



Instead of the correct word "serpent" (tried it only with the first seven-letter group, the same problem occurs when %7 is left out) my code yields a false result beginning with index 4: serpsne.



What is the mistake?



list=['e','r','n','t','e','p','s']
clear=
for x in list:
if list.index(x)%7==0:
a=list[list.index(x)+6]
elif list.index(x)%7==1:
a=list[list.index(x)-1]
elif list.index(x)%7==2:
a=list[list.index(x)-1]
elif list.index(x)%7==3:
a=list[list.index(x)+2]
elif list.index(x)%7==4:
a=x
elif list.index(x)%7==5:
a=list[list.index(x)-3]
else:
a=list[list.index(x)-6]
clear.append(a)
clear=''.join(clear)
print(clear)


(Don´t know why this box inserts two blank lines after for and else, my code has no blank lines.)







python list reindex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 10:45









num3ri

13728




13728










asked Nov 22 at 8:31









Tobi

1




1












  • You could swap the values by index. list[0],list[1] = list[1],list[0]
    – van der Zon Stef
    Nov 22 at 8:36




















  • You could swap the values by index. list[0],list[1] = list[1],list[0]
    – van der Zon Stef
    Nov 22 at 8:36


















You could swap the values by index. list[0],list[1] = list[1],list[0]
– van der Zon Stef
Nov 22 at 8:36






You could swap the values by index. list[0],list[1] = list[1],list[0]
– van der Zon Stef
Nov 22 at 8:36














2 Answers
2






active

oldest

votes

















up vote
1
down vote













Not sure why you're doing this much! Try this one below:



lst=['e','r','n','t','e','p','s']
clear=
key='6015423'
for x in key:
clear.append(lst[int(x)])

clear=''.join(clear)
print(clear)





share|improve this answer





















  • Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
    – Tobi
    Nov 22 at 9:20




















up vote
0
down vote













Because list.index('e') is always 0.



It will find the index of the first occurence of 'e', not the second one and hence it will never execute this:



elif list.index(x)%7==4:
a=x


Try to run this code:



list=['e','r','n','t','e','p','s']
for x in list:
print ( list.index(x))


You will get 0123056 instead of 0123456






share|improve this answer





















  • I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
    – Tobi
    Nov 22 at 9:23










  • sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
    – jlanik
    Nov 22 at 11:45













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',
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%2f53426717%2fhow-to-exchange-the-places-of-the-items-in-a-python-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Not sure why you're doing this much! Try this one below:



lst=['e','r','n','t','e','p','s']
clear=
key='6015423'
for x in key:
clear.append(lst[int(x)])

clear=''.join(clear)
print(clear)





share|improve this answer





















  • Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
    – Tobi
    Nov 22 at 9:20

















up vote
1
down vote













Not sure why you're doing this much! Try this one below:



lst=['e','r','n','t','e','p','s']
clear=
key='6015423'
for x in key:
clear.append(lst[int(x)])

clear=''.join(clear)
print(clear)





share|improve this answer





















  • Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
    – Tobi
    Nov 22 at 9:20















up vote
1
down vote










up vote
1
down vote









Not sure why you're doing this much! Try this one below:



lst=['e','r','n','t','e','p','s']
clear=
key='6015423'
for x in key:
clear.append(lst[int(x)])

clear=''.join(clear)
print(clear)





share|improve this answer












Not sure why you're doing this much! Try this one below:



lst=['e','r','n','t','e','p','s']
clear=
key='6015423'
for x in key:
clear.append(lst[int(x)])

clear=''.join(clear)
print(clear)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 8:38









TeraBaapBC

22912




22912












  • Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
    – Tobi
    Nov 22 at 9:20




















  • Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
    – Tobi
    Nov 22 at 9:20


















Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
– Tobi
Nov 22 at 9:20






Thanks a lot. I used the modulo thing because 'list' , unlike in my example given above, consists of many seven-letter groups, all transposed with the identical key. I'll try to multiply 'key' in your code so that it becomes as long as 'list'.
– Tobi
Nov 22 at 9:20














up vote
0
down vote













Because list.index('e') is always 0.



It will find the index of the first occurence of 'e', not the second one and hence it will never execute this:



elif list.index(x)%7==4:
a=x


Try to run this code:



list=['e','r','n','t','e','p','s']
for x in list:
print ( list.index(x))


You will get 0123056 instead of 0123456






share|improve this answer





















  • I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
    – Tobi
    Nov 22 at 9:23










  • sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
    – jlanik
    Nov 22 at 11:45

















up vote
0
down vote













Because list.index('e') is always 0.



It will find the index of the first occurence of 'e', not the second one and hence it will never execute this:



elif list.index(x)%7==4:
a=x


Try to run this code:



list=['e','r','n','t','e','p','s']
for x in list:
print ( list.index(x))


You will get 0123056 instead of 0123456






share|improve this answer





















  • I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
    – Tobi
    Nov 22 at 9:23










  • sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
    – jlanik
    Nov 22 at 11:45















up vote
0
down vote










up vote
0
down vote









Because list.index('e') is always 0.



It will find the index of the first occurence of 'e', not the second one and hence it will never execute this:



elif list.index(x)%7==4:
a=x


Try to run this code:



list=['e','r','n','t','e','p','s']
for x in list:
print ( list.index(x))


You will get 0123056 instead of 0123456






share|improve this answer












Because list.index('e') is always 0.



It will find the index of the first occurence of 'e', not the second one and hence it will never execute this:



elif list.index(x)%7==4:
a=x


Try to run this code:



list=['e','r','n','t','e','p','s']
for x in list:
print ( list.index(x))


You will get 0123056 instead of 0123456







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 8:46









jlanik

37419




37419












  • I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
    – Tobi
    Nov 22 at 9:23










  • sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
    – jlanik
    Nov 22 at 11:45




















  • I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
    – Tobi
    Nov 22 at 9:23










  • sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
    – jlanik
    Nov 22 at 11:45


















I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
– Tobi
Nov 22 at 9:23




I'll bear this in mind, thanks! Is there a way to find the indices of further occurences of an item?
– Tobi
Nov 22 at 9:23












sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
– jlanik
Nov 22 at 11:45






sure, you can do e.g. all_occurences_of_e = [ i for i in range(len(lst)) if lst[i]=='e' ] by the way it is probably not a good idea to call your list variable 'list', as 'list' is also the name of the class
– jlanik
Nov 22 at 11:45




















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53426717%2fhow-to-exchange-the-places-of-the-items-in-a-python-list%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)