Turning a string of integers into a list and sorting it by odd and even elements
up vote
1
down vote
favorite
I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.
I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.
I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help
python list dictionary
add a comment |
up vote
1
down vote
favorite
I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.
I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.
I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help
python list dictionary
2
But{11, 2, 14, 1, 17, 1, 123, 1}isn't a string of integers.
– jpp
Nov 11 at 2:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.
I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.
I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help
python list dictionary
I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.
I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.
I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help
python list dictionary
python list dictionary
asked Nov 11 at 2:39
Joel Banks
395
395
2
But{11, 2, 14, 1, 17, 1, 123, 1}isn't a string of integers.
– jpp
Nov 11 at 2:46
add a comment |
2
But{11, 2, 14, 1, 17, 1, 123, 1}isn't a string of integers.
– jpp
Nov 11 at 2:46
2
2
But
{11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.– jpp
Nov 11 at 2:46
But
{11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.– jpp
Nov 11 at 2:46
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.
>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
add a comment |
up vote
0
down vote
String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:
from collections import defaultdict
x = {11: 2, 14: 1, 17: 1, 123: 1}
dd = defaultdict(list)
for key in x:
dd['odd' if key % 2 else 'even'].append(key)
The result is a dictionary mapping of odd and even keys:
defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})
You can then access odd keys via dd['odd'], even keys via dd['even'].
add a comment |
up vote
0
down vote
To split a string into ints, use the built-in split method to return a list of each of the items:
new_list = string.split(", ")
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.
>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
add a comment |
up vote
3
down vote
accepted
Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.
>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.
>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]
Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.
>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]
answered Nov 11 at 2:42
UltraInstinct
30.3k76090
30.3k76090
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
add a comment |
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
Perfect, thank you
– Joel Banks
Nov 11 at 2:48
add a comment |
up vote
0
down vote
String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:
from collections import defaultdict
x = {11: 2, 14: 1, 17: 1, 123: 1}
dd = defaultdict(list)
for key in x:
dd['odd' if key % 2 else 'even'].append(key)
The result is a dictionary mapping of odd and even keys:
defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})
You can then access odd keys via dd['odd'], even keys via dd['even'].
add a comment |
up vote
0
down vote
String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:
from collections import defaultdict
x = {11: 2, 14: 1, 17: 1, 123: 1}
dd = defaultdict(list)
for key in x:
dd['odd' if key % 2 else 'even'].append(key)
The result is a dictionary mapping of odd and even keys:
defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})
You can then access odd keys via dd['odd'], even keys via dd['even'].
add a comment |
up vote
0
down vote
up vote
0
down vote
String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:
from collections import defaultdict
x = {11: 2, 14: 1, 17: 1, 123: 1}
dd = defaultdict(list)
for key in x:
dd['odd' if key % 2 else 'even'].append(key)
The result is a dictionary mapping of odd and even keys:
defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})
You can then access odd keys via dd['odd'], even keys via dd['even'].
String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:
from collections import defaultdict
x = {11: 2, 14: 1, 17: 1, 123: 1}
dd = defaultdict(list)
for key in x:
dd['odd' if key % 2 else 'even'].append(key)
The result is a dictionary mapping of odd and even keys:
defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})
You can then access odd keys via dd['odd'], even keys via dd['even'].
answered Nov 11 at 2:51
jpp
83.6k194896
83.6k194896
add a comment |
add a comment |
up vote
0
down vote
To split a string into ints, use the built-in split method to return a list of each of the items:
new_list = string.split(", ")
add a comment |
up vote
0
down vote
To split a string into ints, use the built-in split method to return a list of each of the items:
new_list = string.split(", ")
add a comment |
up vote
0
down vote
up vote
0
down vote
To split a string into ints, use the built-in split method to return a list of each of the items:
new_list = string.split(", ")
To split a string into ints, use the built-in split method to return a list of each of the items:
new_list = string.split(", ")
answered Nov 11 at 2:58
john doe
23
23
add a comment |
add a comment |
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%2f53245383%2fturning-a-string-of-integers-into-a-list-and-sorting-it-by-odd-and-even-elements%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
2
But
{11, 2, 14, 1, 17, 1, 123, 1}isn't a string of integers.– jpp
Nov 11 at 2:46