Big Text file(even a python list can't hold it at all) containing integers as string, can we sort them in...











up vote
0
down vote

favorite













This question already has an answer here:




  • Pythonic Way to Sort a List of Comma Separated Numbers

    4 answers




First of all, please tell me is this possible or not, I am gone through several posts on StackOverflow but didn't find the answer. I am a beginner in python. I have a file name as input.txt.This file is very big in size even a python list cannot hold all the numbers this is why I am creating a text file. Here I have written only a short of it to examine the problem, This contains integers as a string in an unordered manner, I want to sort them. Is this possible using Python? data in the file looks like:



intput.txt ="1 2 3 4 5 3 5 7 9 6 9 12 10 14 15"


and the desired form is:



input.txt ="1 2 3 3 4 5 5 6 7 9 9 10 12 14 15"


this file contains the sum of1,2,3,4,5numbers taken 1,2,3,4,5 at a time in a series. if we cannot sort this file then is it possible to create a sorted file?




for actual big file I think the solution is other than using a list!











share|improve this question















marked as duplicate by Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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();
}
);
});
});
Nov 10 at 9:38


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.















  • What have you tried so far?
    – RoadRunner
    Nov 10 at 9:35










  • Yes, it is possible. What have you tried so far?
    – MisterMiyagi
    Nov 10 at 9:35










  • I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
    – VIKAS RATHEE
    Nov 10 at 9:38










  • If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
    – Useless
    Nov 10 at 17:21










  • Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
    – VIKAS RATHEE
    Nov 11 at 1:48















up vote
0
down vote

favorite













This question already has an answer here:




  • Pythonic Way to Sort a List of Comma Separated Numbers

    4 answers




First of all, please tell me is this possible or not, I am gone through several posts on StackOverflow but didn't find the answer. I am a beginner in python. I have a file name as input.txt.This file is very big in size even a python list cannot hold all the numbers this is why I am creating a text file. Here I have written only a short of it to examine the problem, This contains integers as a string in an unordered manner, I want to sort them. Is this possible using Python? data in the file looks like:



intput.txt ="1 2 3 4 5 3 5 7 9 6 9 12 10 14 15"


and the desired form is:



input.txt ="1 2 3 3 4 5 5 6 7 9 9 10 12 14 15"


this file contains the sum of1,2,3,4,5numbers taken 1,2,3,4,5 at a time in a series. if we cannot sort this file then is it possible to create a sorted file?




for actual big file I think the solution is other than using a list!











share|improve this question















marked as duplicate by Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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();
}
);
});
});
Nov 10 at 9:38


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.















  • What have you tried so far?
    – RoadRunner
    Nov 10 at 9:35










  • Yes, it is possible. What have you tried so far?
    – MisterMiyagi
    Nov 10 at 9:35










  • I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
    – VIKAS RATHEE
    Nov 10 at 9:38










  • If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
    – Useless
    Nov 10 at 17:21










  • Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
    – VIKAS RATHEE
    Nov 11 at 1:48













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Pythonic Way to Sort a List of Comma Separated Numbers

    4 answers




First of all, please tell me is this possible or not, I am gone through several posts on StackOverflow but didn't find the answer. I am a beginner in python. I have a file name as input.txt.This file is very big in size even a python list cannot hold all the numbers this is why I am creating a text file. Here I have written only a short of it to examine the problem, This contains integers as a string in an unordered manner, I want to sort them. Is this possible using Python? data in the file looks like:



intput.txt ="1 2 3 4 5 3 5 7 9 6 9 12 10 14 15"


and the desired form is:



input.txt ="1 2 3 3 4 5 5 6 7 9 9 10 12 14 15"


this file contains the sum of1,2,3,4,5numbers taken 1,2,3,4,5 at a time in a series. if we cannot sort this file then is it possible to create a sorted file?




for actual big file I think the solution is other than using a list!











share|improve this question
















This question already has an answer here:




  • Pythonic Way to Sort a List of Comma Separated Numbers

    4 answers




First of all, please tell me is this possible or not, I am gone through several posts on StackOverflow but didn't find the answer. I am a beginner in python. I have a file name as input.txt.This file is very big in size even a python list cannot hold all the numbers this is why I am creating a text file. Here I have written only a short of it to examine the problem, This contains integers as a string in an unordered manner, I want to sort them. Is this possible using Python? data in the file looks like:



intput.txt ="1 2 3 4 5 3 5 7 9 6 9 12 10 14 15"


and the desired form is:



input.txt ="1 2 3 3 4 5 5 6 7 9 9 10 12 14 15"


this file contains the sum of1,2,3,4,5numbers taken 1,2,3,4,5 at a time in a series. if we cannot sort this file then is it possible to create a sorted file?




for actual big file I think the solution is other than using a list!






This question already has an answer here:




  • Pythonic Way to Sort a List of Comma Separated Numbers

    4 answers








python file data-structures integer bigdata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 13:03

























asked Nov 10 at 9:31









VIKAS RATHEE

399




399




marked as duplicate by Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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();
}
);
});
});
Nov 10 at 9:38


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 Jean-François Fabre python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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();
}
);
});
});
Nov 10 at 9:38


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.














  • What have you tried so far?
    – RoadRunner
    Nov 10 at 9:35










  • Yes, it is possible. What have you tried so far?
    – MisterMiyagi
    Nov 10 at 9:35










  • I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
    – VIKAS RATHEE
    Nov 10 at 9:38










  • If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
    – Useless
    Nov 10 at 17:21










  • Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
    – VIKAS RATHEE
    Nov 11 at 1:48


















  • What have you tried so far?
    – RoadRunner
    Nov 10 at 9:35










  • Yes, it is possible. What have you tried so far?
    – MisterMiyagi
    Nov 10 at 9:35










  • I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
    – VIKAS RATHEE
    Nov 10 at 9:38










  • If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
    – Useless
    Nov 10 at 17:21










  • Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
    – VIKAS RATHEE
    Nov 11 at 1:48
















What have you tried so far?
– RoadRunner
Nov 10 at 9:35




What have you tried so far?
– RoadRunner
Nov 10 at 9:35












Yes, it is possible. What have you tried so far?
– MisterMiyagi
Nov 10 at 9:35




Yes, it is possible. What have you tried so far?
– MisterMiyagi
Nov 10 at 9:35












I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
– VIKAS RATHEE
Nov 10 at 9:38




I am using sort function of as 'print(input.txt.sort()) but it is giving me an error str function do not have sort attribute.
– VIKAS RATHEE
Nov 10 at 9:38












If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
– Useless
Nov 10 at 17:21




If your file really can't fit in memory, this is a much harder problem. How big is it? Is there any limit on the range of values it can contain?
– Useless
Nov 10 at 17:21












Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
– VIKAS RATHEE
Nov 11 at 1:48




Yes, its size is 17,112,592,500 and python list max size is approximately 536,870,912
– VIKAS RATHEE
Nov 11 at 1:48












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Sure, you can use this list comprehension:



tex = (sorted([int(x) for x in text.split(",")]))


text.slit(",") returns:



['1', '2', '3', '4', '5', '3', '5', '7', '9', '6', '9', '12', '10', '14', '15']


With the list comprehension you turn these strings into integers and finally you sort these.



You can convert this back into a the desired string using:



print(",".join([str(x) for x in tex]))


But actually it is not necessary to convert to integers at all since the strings get sorted according to ancii order. Therefore this would suffice:



print("".join(sorted(tex.split(","))))





share|improve this answer



















  • 1




    don't forget to join the values back into strings with commas
    – Jean-François Fabre
    Nov 10 at 9:37










  • means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
    – VIKAS RATHEE
    Nov 10 at 9:43












  • To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
    – petruz
    Nov 10 at 9:52












  • this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
    – VIKAS RATHEE
    Nov 10 at 10:14












  • naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
    – petruz
    Nov 10 at 20:45


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Sure, you can use this list comprehension:



tex = (sorted([int(x) for x in text.split(",")]))


text.slit(",") returns:



['1', '2', '3', '4', '5', '3', '5', '7', '9', '6', '9', '12', '10', '14', '15']


With the list comprehension you turn these strings into integers and finally you sort these.



You can convert this back into a the desired string using:



print(",".join([str(x) for x in tex]))


But actually it is not necessary to convert to integers at all since the strings get sorted according to ancii order. Therefore this would suffice:



print("".join(sorted(tex.split(","))))





share|improve this answer



















  • 1




    don't forget to join the values back into strings with commas
    – Jean-François Fabre
    Nov 10 at 9:37










  • means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
    – VIKAS RATHEE
    Nov 10 at 9:43












  • To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
    – petruz
    Nov 10 at 9:52












  • this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
    – VIKAS RATHEE
    Nov 10 at 10:14












  • naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
    – petruz
    Nov 10 at 20:45















up vote
2
down vote













Sure, you can use this list comprehension:



tex = (sorted([int(x) for x in text.split(",")]))


text.slit(",") returns:



['1', '2', '3', '4', '5', '3', '5', '7', '9', '6', '9', '12', '10', '14', '15']


With the list comprehension you turn these strings into integers and finally you sort these.



You can convert this back into a the desired string using:



print(",".join([str(x) for x in tex]))


But actually it is not necessary to convert to integers at all since the strings get sorted according to ancii order. Therefore this would suffice:



print("".join(sorted(tex.split(","))))





share|improve this answer



















  • 1




    don't forget to join the values back into strings with commas
    – Jean-François Fabre
    Nov 10 at 9:37










  • means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
    – VIKAS RATHEE
    Nov 10 at 9:43












  • To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
    – petruz
    Nov 10 at 9:52












  • this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
    – VIKAS RATHEE
    Nov 10 at 10:14












  • naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
    – petruz
    Nov 10 at 20:45













up vote
2
down vote










up vote
2
down vote









Sure, you can use this list comprehension:



tex = (sorted([int(x) for x in text.split(",")]))


text.slit(",") returns:



['1', '2', '3', '4', '5', '3', '5', '7', '9', '6', '9', '12', '10', '14', '15']


With the list comprehension you turn these strings into integers and finally you sort these.



You can convert this back into a the desired string using:



print(",".join([str(x) for x in tex]))


But actually it is not necessary to convert to integers at all since the strings get sorted according to ancii order. Therefore this would suffice:



print("".join(sorted(tex.split(","))))





share|improve this answer














Sure, you can use this list comprehension:



tex = (sorted([int(x) for x in text.split(",")]))


text.slit(",") returns:



['1', '2', '3', '4', '5', '3', '5', '7', '9', '6', '9', '12', '10', '14', '15']


With the list comprehension you turn these strings into integers and finally you sort these.



You can convert this back into a the desired string using:



print(",".join([str(x) for x in tex]))


But actually it is not necessary to convert to integers at all since the strings get sorted according to ancii order. Therefore this would suffice:



print("".join(sorted(tex.split(","))))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 20:35

























answered Nov 10 at 9:35









petruz

461311




461311








  • 1




    don't forget to join the values back into strings with commas
    – Jean-François Fabre
    Nov 10 at 9:37










  • means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
    – VIKAS RATHEE
    Nov 10 at 9:43












  • To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
    – petruz
    Nov 10 at 9:52












  • this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
    – VIKAS RATHEE
    Nov 10 at 10:14












  • naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
    – petruz
    Nov 10 at 20:45














  • 1




    don't forget to join the values back into strings with commas
    – Jean-François Fabre
    Nov 10 at 9:37










  • means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
    – VIKAS RATHEE
    Nov 10 at 9:43












  • To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
    – petruz
    Nov 10 at 9:52












  • this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
    – VIKAS RATHEE
    Nov 10 at 10:14












  • naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
    – petruz
    Nov 10 at 20:45








1




1




don't forget to join the values back into strings with commas
– Jean-François Fabre
Nov 10 at 9:37




don't forget to join the values back into strings with commas
– Jean-François Fabre
Nov 10 at 9:37












means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
– VIKAS RATHEE
Nov 10 at 9:43






means first I have to open this file fhand = open("input.txt") and then read its all data line by line right?
– VIKAS RATHEE
Nov 10 at 9:43














To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
– petruz
Nov 10 at 9:52






To open the file use 'with open("pathfile", "r") as f: text = f.readline()'. If you use windows you have to change the backslashes of the pathfile.
– petruz
Nov 10 at 9:52














this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
– VIKAS RATHEE
Nov 10 at 10:14






this helps me thank you, can we write the result in a text file also? this is only a short example of my big data the actual file contains data more than python list can hold can we sort that also by using this way.
– VIKAS RATHEE
Nov 10 at 10:14














naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
– petruz
Nov 10 at 20:45




naturally: 'with open('pathfile', 'w') as newfile: newfile.write("".join(sorted(tex.split(","))))' would suffice. Be aware that there is actually no need to convert to integers to sort at all.
– petruz
Nov 10 at 20:45



這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini