Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am doing a project on autonomous vehicle which travels to recommended gps coordinates. How should I create a new list and enter the output values of a program to that list? My program is as below here, I get the line numbers which have have the string $GPRMC. I want to store the line numbers into a list and manipulate those lines further.
f=open('c:\Users\RuthvikWalia\Desktop\gpsdata.txt ','r')
req_lines=0
for line in f:
if(line.find('$GPRMC') >=0 ):
print 'its here', req_lines
req_lines += 1
p=[]
p= p.append(req_lines)
print p
I get the output of only the numbers of lines which have $GPRMC but not the list of numbers of those lines.
This will put tuples of (line_number, line_string) into the list req_lines:
f = open('c:\Users\RuthvikWalia\Desktop\gpsdata.txt ','r')
req_lines = []
for i, line in enumerate(f):
if "$GPRMC" in line:
req_lines.append((i, line))
print req_lines
#Claudiu answer is correct, just to give a small taste of what python's really like, do:
[(idx,line) for idx,line in enumerate(open('gpsdata.txt')) if '$GPRMC' in line]
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried these, but its only for one column.
using notepad++ and python. Any solution that can do for multiple columns of text?
# Input:
tim
oso
d n
a d
y a
y
# Output:
today
is
monday
You can do this fairly easily with a loop, something like:
s = "tim\noso\nd n\na d\ny a\n y"
bits = s.split('\n')
lines = ['' for i in range(0, len(bits[0]))]
for bit in bits:
for i in range(0, len(bit)):
if bit[i] != ' ':
lines[i] += bit[i]
'\n'.join(lines)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a file that contains the following data:
1,0.00008118
1,0.00008101
12,0.00008122
5,0.00008091
8,0.00008092
1,0.00008109
2,0.00008100
260,0.00008087
248,0.00007982
69,0.00007958
71,0.00007954
51,0.00007910
20,0.00007932
I'd like to be able to sort the file so that the integer values are sorted lowest to highest. When sorting using the sorted function, it appears to be sorting numerically instead.
a_s = sorted(List)
for i in a_s:
print(i)
So the output is:
1,0.00008101
1,0.00008109
1,0.00008118
12,0.00008122
2,0.00008100
20,0.00007932
248,0.00007982
260,0.00008087
5,0.00008091
51,0.00007910
69,0.00007958
71,0.00007954
8,0.00008092
How would i get it so that the numerical values are sorted the lowest to highest value?
Many Thanks,
Try this:
with open('test.txt') as fp:
data = [line.strip().split(',') for line in fp]
res = '\n'.join([','.join(x) for x in sorted(data, key=lambda x: int(x[0]))])
print(res)
Output:
1,0.00008118
1,0.00008101
1,0.00008109
2,0.00008100
5,0.00008091
8,0.00008092
12,0.00008122
20,0.00007932
51,0.00007910
69,0.00007958
71,0.00007954
248,0.00007982
260,0.00008087
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to read from stdin and the first line specify the dimension of matrix always square matrix
but my code reads only the first line
Example Input
3
0 2 3
2 4 1
3 1 4
My code
def read_matrix(formatted_string):
list_of_lists = [list(map(int, row.split())) for row in formatted_string.split('\n')]
return list_of_lists
x = input("enter the list of lists of numbers?")
print(read_matrix(x))
You're only asking the number of rows but not the content of rows themselves. Following code shows how to generate the full matrix:
def read_matrix(count):
list_of_lists = [list(map(int, input('Enter a row: ').split())) for _ in range(count)]
return list_of_lists
x = input("enter the list of lists of numbers?")
print(read_matrix(int(x)))
It will first query the user the number of rows and pass that number to read_matrix which will ask the user to input every line.
According to the official documentation,
The function then reads a line from input,
converts it to a string (stripping a trailing newline)
Please, note reads a line. But here you are trying to read 4 lines with one call to input. You might want to run a loop to take this input.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I had a file called 'words.txt' which contains things such as. #+=%£%= and i need to go through and count each symbol without allowing any duplicates and then print my answer. for example it should look like this when printed:
# : 1
+ : 1
= : 2
% : 2
£ : 1
I know I need to use a for loop in this and that I need to use a set so It doesnt't allow any duplicates but how would I do it? Thanl you
A set isn't so useful here as it doesn't have a place to store the counts
from collections import Counter
with open("words.txt") as fin:
c = Counter(fin.read())
for item in c.items():
print("{} : {}".format(*item))
Use python dictionary:
symbols = {}
for c in string:
if c not in symbols:
symbols[c] = 0
symbols[c] += 1
Look up dictionary.
Don't want to post more as it would only spoil the exercise.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have this code right here:
myfile = open("chess.txt", 'r')
line = myfile.readline().rstrip('\n')
while line != '':
print(line.rstrip('\n'))
line = myfile.readline().rstrip('\n')
myfile.close()
and it prints out this:
1692 The Rickster
2875 Gary Kasparov
1692 Bobby Fisher
1235 Ben Dover
0785 Chuck Roast
1010 Jim Naysium
0834 Baba Wawa
1616 Bruce Lee
0123 K. T. Frog
2000 Socrates
What do I need to use to arrange them in order from highest to lowest (numbers)?
the myfile is the list of names and numbers put on a notepad.
Read your lines into a list of tuples with the score converted to an integer for ease of sorting numerically, then sort the list:
entries = []
with open('chess.txt') as chessfile:
for line in chessfile:
score, name = line.strip().split(' ', 1)
entries.append((int(score), name))
entries.sort(reverse=True)
That said, your lines, with the 0-padded integers at the front, will sort lexicographically as well:
with open('chess.txt') as chessfile:
entries = list(chessfile)
entries.sort(reverse=True)
This version will work even if the numbers are not 0-padded.
To avoid having to add the key onto the line, use the "key" argument to sorted:
with open('/tmp/chess.txt') as chessfile:
print ''.join(sorted(chessfile, reverse=True,
key=lambda k: int(k.split()[0])))