How to sort a string list having both number and alphabets according to the highest number [duplicate]
This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
1 answer
I am looking to sort a string list numerically which has both string and number also as a string.
['har 1', 'zee 3']
expected output :
zee 3
har 1
python-3.x
marked as duplicate by DeepSpace, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
1 answer
I am looking to sort a string list numerically which has both string and number also as a string.
['har 1', 'zee 3']
expected output :
zee 3
har 1
python-3.x
marked as duplicate by DeepSpace, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
1 answer
I am looking to sort a string list numerically which has both string and number also as a string.
['har 1', 'zee 3']
expected output :
zee 3
har 1
python-3.x
This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
1 answer
I am looking to sort a string list numerically which has both string and number also as a string.
['har 1', 'zee 3']
expected output :
zee 3
har 1
This question already has an answer here:
How to correctly sort a string with a number inside? [duplicate]
1 answer
python-3.x
python-3.x
asked Jan 2 at 14:46
Zeeshan Haris Zeeshan Haris
62
62
marked as duplicate by DeepSpace, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by DeepSpace, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
add a comment |
You can sort your list with the sorted and the argument key
l = ['har 1', 'zee 3']
print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))
Output
['zee 3', 'har 1']
You can even print it as you wanted with the unpack operator
print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')
Here I am creating from each value a tuple of (-int(second_element), first_element)
So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically
add a comment |
You can use the key
parameter of the sorted function to specify a custom sorting order -
l = ['X 3', 'Z 1', 'A 2']
l_sorted = sorted(l, key=lambda x: x.split()[-1])
print(l_sorted)
/*Returns ['Z 1', 'A 2', 'X 3']*/
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
add a comment |
You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
add a comment |
You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
You can use key-value (dictionary) for this. Then simply sort your list according to key/value using methods given in https://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/
answered Jan 2 at 14:53
Sham GirSham Gir
11
11
add a comment |
add a comment |
You can sort your list with the sorted and the argument key
l = ['har 1', 'zee 3']
print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))
Output
['zee 3', 'har 1']
You can even print it as you wanted with the unpack operator
print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')
Here I am creating from each value a tuple of (-int(second_element), first_element)
So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically
add a comment |
You can sort your list with the sorted and the argument key
l = ['har 1', 'zee 3']
print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))
Output
['zee 3', 'har 1']
You can even print it as you wanted with the unpack operator
print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')
Here I am creating from each value a tuple of (-int(second_element), first_element)
So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically
add a comment |
You can sort your list with the sorted and the argument key
l = ['har 1', 'zee 3']
print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))
Output
['zee 3', 'har 1']
You can even print it as you wanted with the unpack operator
print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')
Here I am creating from each value a tuple of (-int(second_element), first_element)
So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically
You can sort your list with the sorted and the argument key
l = ['har 1', 'zee 3']
print(sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])))
Output
['zee 3', 'har 1']
You can even print it as you wanted with the unpack operator
print(*sorted(l, key=lambda x: (-int(x.split()[1]), x.split()[0])), sep='n')
Here I am creating from each value a tuple of (-int(second_element), first_element)
So it will sort decreasingly by the numbers, and in case of equality, it will sort alphabetically
answered Jan 2 at 14:57
BlueSheepTokenBlueSheepToken
1,826517
1,826517
add a comment |
add a comment |
You can use the key
parameter of the sorted function to specify a custom sorting order -
l = ['X 3', 'Z 1', 'A 2']
l_sorted = sorted(l, key=lambda x: x.split()[-1])
print(l_sorted)
/*Returns ['Z 1', 'A 2', 'X 3']*/
add a comment |
You can use the key
parameter of the sorted function to specify a custom sorting order -
l = ['X 3', 'Z 1', 'A 2']
l_sorted = sorted(l, key=lambda x: x.split()[-1])
print(l_sorted)
/*Returns ['Z 1', 'A 2', 'X 3']*/
add a comment |
You can use the key
parameter of the sorted function to specify a custom sorting order -
l = ['X 3', 'Z 1', 'A 2']
l_sorted = sorted(l, key=lambda x: x.split()[-1])
print(l_sorted)
/*Returns ['Z 1', 'A 2', 'X 3']*/
You can use the key
parameter of the sorted function to specify a custom sorting order -
l = ['X 3', 'Z 1', 'A 2']
l_sorted = sorted(l, key=lambda x: x.split()[-1])
print(l_sorted)
/*Returns ['Z 1', 'A 2', 'X 3']*/
answered Jan 2 at 14:57
MortzMortz
795519
795519
add a comment |
add a comment |