How can I sort the list by size?
I am new here and new to Python.
I would like to know if anyone knows how to sort a generated list by size.
I have a piece of the code here. The sorting should be from small to large.
6.52, 26.4
for i in x:
pfad = (i)
title=(i)
size = (i)
bild = (i)
liste=('<a href="' + pfad + '">' + title + '<br>;' + size + '<div><img src="' + bild + '" /></div></a></br>')
print(liste)
<a href="URL/">Title<br>;26.14;GB<div><img src="https://URL.jpg" /></div></a></br>
<a href="URL/">Title<br>;6.52;GB<div><img src="https://URL.jpg" /></div></a></br>
<and much more>
I tried with
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste,key=operator.itemgetter(1))
for i in liste:
print (i)
and other things (lambda and so on) but it did not work.
Does anyone have a suggestion?
Greeting Tron
python-3.x list
add a comment |
I am new here and new to Python.
I would like to know if anyone knows how to sort a generated list by size.
I have a piece of the code here. The sorting should be from small to large.
6.52, 26.4
for i in x:
pfad = (i)
title=(i)
size = (i)
bild = (i)
liste=('<a href="' + pfad + '">' + title + '<br>;' + size + '<div><img src="' + bild + '" /></div></a></br>')
print(liste)
<a href="URL/">Title<br>;26.14;GB<div><img src="https://URL.jpg" /></div></a></br>
<a href="URL/">Title<br>;6.52;GB<div><img src="https://URL.jpg" /></div></a></br>
<and much more>
I tried with
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste,key=operator.itemgetter(1))
for i in liste:
print (i)
and other things (lambda and so on) but it did not work.
Does anyone have a suggestion?
Greeting Tron
python-3.x list
2
Hi there, welcome to SO.I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.
– TrebledJ
Nov 16 '18 at 3:34
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13
add a comment |
I am new here and new to Python.
I would like to know if anyone knows how to sort a generated list by size.
I have a piece of the code here. The sorting should be from small to large.
6.52, 26.4
for i in x:
pfad = (i)
title=(i)
size = (i)
bild = (i)
liste=('<a href="' + pfad + '">' + title + '<br>;' + size + '<div><img src="' + bild + '" /></div></a></br>')
print(liste)
<a href="URL/">Title<br>;26.14;GB<div><img src="https://URL.jpg" /></div></a></br>
<a href="URL/">Title<br>;6.52;GB<div><img src="https://URL.jpg" /></div></a></br>
<and much more>
I tried with
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste,key=operator.itemgetter(1))
for i in liste:
print (i)
and other things (lambda and so on) but it did not work.
Does anyone have a suggestion?
Greeting Tron
python-3.x list
I am new here and new to Python.
I would like to know if anyone knows how to sort a generated list by size.
I have a piece of the code here. The sorting should be from small to large.
6.52, 26.4
for i in x:
pfad = (i)
title=(i)
size = (i)
bild = (i)
liste=('<a href="' + pfad + '">' + title + '<br>;' + size + '<div><img src="' + bild + '" /></div></a></br>')
print(liste)
<a href="URL/">Title<br>;26.14;GB<div><img src="https://URL.jpg" /></div></a></br>
<a href="URL/">Title<br>;6.52;GB<div><img src="https://URL.jpg" /></div></a></br>
<and much more>
I tried with
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste,key=operator.itemgetter(1))
for i in liste:
print (i)
and other things (lambda and so on) but it did not work.
Does anyone have a suggestion?
Greeting Tron
python-3.x list
python-3.x list
edited Nov 16 '18 at 9:29
Akhilesh Pandey
549314
549314
asked Nov 16 '18 at 3:30
Tron0070Tron0070
11
11
2
Hi there, welcome to SO.I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.
– TrebledJ
Nov 16 '18 at 3:34
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13
add a comment |
2
Hi there, welcome to SO.I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.
– TrebledJ
Nov 16 '18 at 3:34
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13
2
2
Hi there, welcome to SO.
I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.– TrebledJ
Nov 16 '18 at 3:34
Hi there, welcome to SO.
I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.– TrebledJ
Nov 16 '18 at 3:34
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13
add a comment |
2 Answers
2
active
oldest
votes
If you are sure of getting just a simple list like [1, 2, 3, 4]
then, using sorted()
method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True
into sorting method.
You can retrieve an element from a list with itemgetter()
like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)
add a comment |
thank you for your help.
I have now a example code.
#!/usr/bin/env python3
import csv
liste = ['Name:1 ;26.1; E-a', 'Name:2 ;262; E-b', 'Name:3 ;7.92; E-c', 'Name:4 ;4.6; E-d', 'Name:5 ;4.74; E-e']
liste_csv = csv.reader(liste,delimiter=';')
liste_sort = sorted(liste_csv, key=lambda k: k[1])
for i in liste_sort:
print (i)
I get the following issue
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
But I want
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
How can I do that?
Sorry for the bumpy start.
I just started learning Python.
Greetings Tron
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
add a comment |
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
});
}
});
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%2f53331006%2fhow-can-i-sort-the-list-by-size%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
If you are sure of getting just a simple list like [1, 2, 3, 4]
then, using sorted()
method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True
into sorting method.
You can retrieve an element from a list with itemgetter()
like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)
add a comment |
If you are sure of getting just a simple list like [1, 2, 3, 4]
then, using sorted()
method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True
into sorting method.
You can retrieve an element from a list with itemgetter()
like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)
add a comment |
If you are sure of getting just a simple list like [1, 2, 3, 4]
then, using sorted()
method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True
into sorting method.
You can retrieve an element from a list with itemgetter()
like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)
If you are sure of getting just a simple list like [1, 2, 3, 4]
then, using sorted()
method is in ascending order by default, so no need of a key.
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste)
for i in liste:
print (i)
for descending order add reverse = True
into sorting method.
You can retrieve an element from a list with itemgetter()
like this:
>>> from operator import itemgetter
>>> z = ['foo', 'bar','qux','zoo']
>>> itemgetter(1)(z)
If still you want to sort with itemgetter you need to have atleast 2 dimensional list
>>> a = [[1,2,1,2,3,4,5,6,3,4],[1,2,3,4],[4,5,7,8], [1,2,2,3,4,5,4,65,3,4,3,2,2]]
>>> sorted(a, key=itemgetter(3))
[[1, 2, 1, 2, 3, 4, 5, 6, 3, 4], [1, 2, 2, 3, 4, 5, 4, 65, 3, 4, 3, 2, 2], [1, 2, 3, 4], [4, 5, 7, 8]]
>>>
But I assume that csv reader will return a dictionary most probably
liste = csv.reader(liste,delimiter=';')
liste = sorted(liste, key=lambda k: k['name of the column'])
for i in liste:
print (i)
answered Nov 16 '18 at 4:55
RarblackRarblack
2,98341227
2,98341227
add a comment |
add a comment |
thank you for your help.
I have now a example code.
#!/usr/bin/env python3
import csv
liste = ['Name:1 ;26.1; E-a', 'Name:2 ;262; E-b', 'Name:3 ;7.92; E-c', 'Name:4 ;4.6; E-d', 'Name:5 ;4.74; E-e']
liste_csv = csv.reader(liste,delimiter=';')
liste_sort = sorted(liste_csv, key=lambda k: k[1])
for i in liste_sort:
print (i)
I get the following issue
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
But I want
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
How can I do that?
Sorry for the bumpy start.
I just started learning Python.
Greetings Tron
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
add a comment |
thank you for your help.
I have now a example code.
#!/usr/bin/env python3
import csv
liste = ['Name:1 ;26.1; E-a', 'Name:2 ;262; E-b', 'Name:3 ;7.92; E-c', 'Name:4 ;4.6; E-d', 'Name:5 ;4.74; E-e']
liste_csv = csv.reader(liste,delimiter=';')
liste_sort = sorted(liste_csv, key=lambda k: k[1])
for i in liste_sort:
print (i)
I get the following issue
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
But I want
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
How can I do that?
Sorry for the bumpy start.
I just started learning Python.
Greetings Tron
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
add a comment |
thank you for your help.
I have now a example code.
#!/usr/bin/env python3
import csv
liste = ['Name:1 ;26.1; E-a', 'Name:2 ;262; E-b', 'Name:3 ;7.92; E-c', 'Name:4 ;4.6; E-d', 'Name:5 ;4.74; E-e']
liste_csv = csv.reader(liste,delimiter=';')
liste_sort = sorted(liste_csv, key=lambda k: k[1])
for i in liste_sort:
print (i)
I get the following issue
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
But I want
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
How can I do that?
Sorry for the bumpy start.
I just started learning Python.
Greetings Tron
thank you for your help.
I have now a example code.
#!/usr/bin/env python3
import csv
liste = ['Name:1 ;26.1; E-a', 'Name:2 ;262; E-b', 'Name:3 ;7.92; E-c', 'Name:4 ;4.6; E-d', 'Name:5 ;4.74; E-e']
liste_csv = csv.reader(liste,delimiter=';')
liste_sort = sorted(liste_csv, key=lambda k: k[1])
for i in liste_sort:
print (i)
I get the following issue
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
But I want
['Name:4 ', '4.6', ' E-d']
['Name:5 ', '4.74', ' E-e']
['Name:3 ', '7.92', ' E-c']
['Name:1 ', '26.1', ' E-a']
['Name:2 ', '262', ' E-b']
How can I do that?
Sorry for the bumpy start.
I just started learning Python.
Greetings Tron
answered Dec 6 '18 at 15:52
Tron0070Tron0070
11
11
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
add a comment |
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
it's been dealt with. I have solve it with sorted(liste_csv, key=lambda k: float(k[1]))
– Tron0070
Dec 7 '18 at 10:06
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.
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%2f53331006%2fhow-can-i-sort-the-list-by-size%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
Hi there, welcome to SO.
I tried with... other things (lambda and so on).
Include in your question what you tried, it will help us. If you haven't done so already, check out the SO tour and how to ask a good question.– TrebledJ
Nov 16 '18 at 3:34
You also might want to provide us sample input with expected output. This makes things a bit clearer.
– Mayank Porwal
Nov 16 '18 at 4:13