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.)
python list reindex
add a comment |
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.)
python list reindex
You could swap the values by index. list[0],list[1] = list[1],list[0]
– van der Zon Stef
Nov 22 at 8:36
add a comment |
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.)
python list reindex
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
python list reindex
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
add a comment |
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
add a comment |
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)
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
add a comment |
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
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You could swap the values by index. list[0],list[1] = list[1],list[0]
– van der Zon Stef
Nov 22 at 8:36