the first element of the list is a string and the rest are numbers. I want to perform mathematical operations between the numbers like median etc but I can't remove the first element a.k.a the string because I keep getting this error
AttributeError: 'str' object has no attribute 'pop'
I tried pop, remove etc
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
for line in infile:
alter = line.split()[0]
alter.pop()
statistics.median(alter)
print(Alter)
Alter
28
25
28
26
22
20
25
21
21
25
24
25
26
22
26
20
27
22
22
26
23
20
22
26
24
22
20
20
19
21
19
19
33
23
21
29
21
25
26
19
23
20
25
21
input(alter)
output = 22.5 the median
Simply skip the first line. next consumes one element of the file iterator:
import statistics
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
header = next(infile)
data = [int(line.split()[0]) for line in infile]
print(statistics.median(data))
Output:
22.5
Your mistake is taking the first element of the List at line.split()[0].
If each line is formed by a string followed by numbers, and you just want to remove the first string, it should be done like this:
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
for line in infile:
alter = line.split()
alter.pop(0)
statistics.median(line)
Notice as well that you must introduce an index of the position of the List that you want to delete with pop().
Get rid of the string by not including the 0 index, then convert the strings representing numbers to actual floats so that they can be used in your calculations.
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as file:
for line in file:
numbers = line.split()[1:]
numbers = map(float, numbers)
statistics.median(numbers)
You can use something like this:
import statistics
list_ = []
with open('1.txt') as infile:
for line in infile:
if line.split()[0].isdigit():
list_.append(int(line.split()[0]))
statistics.median(list_)
test_list = ["bad", 2, 4, 6 ,1, 3]
print ("Original list : " + str(test_list))
R = "bad"
while(R in test_list) :
test_list.remove(R)
print ("List after removing string " + str(test_list))
my_list = ["one", "two", "three", "four", "five", "six"]
print (my_list)
let's say i want the remove one and four
my_list.pop(3)
my_list.pop(0)
print(my_list)
#The output will be
["two", "three", "five", "six"]
Related
This is my data file (called “studentdata.txt”)
joe 10 15 20 30 40
bill 23 16 19 22
sue 8 22 17 14 32 17 24 21 2 9 11 17
grace 12 28 21 45 26 10
john 14 32 25 16 89
I need to calculate the average grade for each student and print out the student’s name along with their average grade. I can extract the name with no problem and determine the number of exam scores, but I can not figure out how to sum the exam scores. This is what I have so far:
file=open("studentdata.txt","r")
for aline in file:
data=aline.split()
print((data[0]),"Average grade:")
print(len(data[1:]))
file.close()
It seems like you have most of this already done, and you already have a good grasp of how to partition each line into the two components you need, so you're real close!
First, since the data is being read in as a string, you need to convert part of your data to integers:
for line in file:
tmp = line.split()
name, scores = tmp[0], list(map(int, tmp[1:]))
This will give us each name, along with a list of scores as integers. Now all you have to do is find the average:
average = sum(scores)/len(scores)
Let's tie it all together by assigning to a dictionary:
dct[name] = average
And we get:
{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}
Try this?
file = open("studentdata.txt", "r")
for aline in file:
data = aline.split()
# Convert list of string numerals to int
grades = [int(grade) for grade in data[1:]]
# Find average by dividing sum by length of numbers list
average = sum(grades)/len(data[1:])
print((data[0]), "Average grade:", str(average))
file.close()
Try the below code, just split each line on spaces then get the numbers not with the name so the indexing will be the i.strip().split()[1:] then use map to convert that into an integer then use statistics.mean to get the average:
from statistics import mean
d = {}
with open('studentdata.txt','r') as f:
for i in f.readlines():
d[i.split()[0]] = mean(list(map(int,i.strip().split()[1:])))
print(d)
Output:
{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}
As in with out outer scope variables, how do you loop getting the first element forward with one var and another var for the last element backward with the same loop? Is there a function similar to enumerate that returns two vars
you could zip one iterator and its reverse:
z = range(20,30)
for x,y in zip(z,reversed(z)):
print(x,y)
results in:
20 29
21 28
22 27
23 26
24 25
25 24
26 23
27 22
28 21
29 20
With generators, though, you have to force iteration into a list since reverse expects a sequence
TypeError: argument to reversed() must be a sequence
list, tuple or range are OK.
g = (x for x in somefunc() if x > 0)
lst = list(g)
for x,y in zip(lst, reversed(lst)):
...
I have txt file witht the following txt in it:
2
4 8 15 16 23 42
1 3 5
6
66 77
77
888
888 77
34
23 234 234
1
32
3
23 23 23
365
22 12
I need a way to read the file and sum all the numbers.
i have this code for now but not sure what to do next. Thx in advance
`lstComplete = []
fichNbr = open("nombres.txt", "r")
lstComplete = fichNbr
somme = 0
for i in lstComplete:
i = i.split()`
Turn them into a list and sum them:
with open('nombres.txt', 'r') as f:
num_list = f.read().split()
print sum([int(n) for n in num_list])
Returns 3227
Open the file and use read() method to get the content and then convert string to int, use sum() to get the result:
>>> sum(map(int,open('nombres.txt').read().split()))
3227
I would like to read in a file with multiple columns and write out a new file with columns in a different order than the original file. One of the columns has some extra text that I want eliminated in the new file as well.
For instance, if I read in file: data.txt
1 6 omi=11 16 21 26
2 7 omi=12 17 22 27
3 8 omi=13 18 23 28
4 9 omi=14 19 24 29
5 10 omi=15 20 25 30
I would like the written file to be: dataNEW.txt
26 1 11 16
27 2 12 17
28 3 13 18
29 4 14 19
30 5 15 20
With the help of inspectorG4dget, I came up with this:
import csv as csv
import sys as sys
infile = open('Rearrange Column Test.txt')
sys.stdout = open('Rearrange Column TestNEW.txt' , 'w')
for line in csv.reader(infile, delimiter='\t'):
newline = [line[i] for i in [5, 0, 2, 3]]
newline[2] = newline[2].split('=')[1]
print newline[0], newline[1], newline[2], newline[3]
sys.stdout.close()
Is there a more concise way to get an output without any commas than listing each line index from 0 to the total number of lines?
import csv
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
writer = csv.writer(outfile)
for line in csv.reader(infile, delimiter='\t'):
newline = [line[i] for i in [-1, 0, 2 3]]
newline[2] = newline[2].split('=')[1]
writer.writerow(newline)
Suppose I have a text file that looks like this:
33 3
46 12
23 10 23 11 23 12 23 13 23 14 23 15 23 16 24 10 24 11 24 12 24 13 24 14 24 15 24 16 25 14 25 15 25 16 26 16 27 16 28 16 29 16
33 17 33 18 33 19 34 17 34 18 34 19 35 17 35 18 35 19 36 19
41 32 41 33 42 32 42 33
I would like to read each line into a separate array of integers, as in (pseudo code):
for line in textfile:
currentArray = firstLine
do stuff with currentArray
where in the first iteration, currentArray would be
array([33, 3])
and in the second iteration, currentArray would be
array([46, 12])
until the last iteration, when currentArray would be
array([41, 32, 41, 33, 42, 32, 42, 33])
Basically, I would like to have the functionality of the numpy function loadtxt:
currentArray = loadtxt('scienceVertices.txt', usecols=() )
Except instead of usecols, being able to specify the row, e.g.,
currentArray = loadtxt('scienceVertices.txt', userows=(line) )
Here's a one-liner:
arrays = [np.array(map(int, line.split())) for line in open('scienceVertices.txt')]
arrays is a list of numpy arrays.
for line in textfile:
a = np.array([int(v) for v in line.strip().split(" ")])
# Work on your array
You can also use numpy.fromstring()
for line in f:
a = numpy.fromstring(line.strip(), dtype=int, sep=" ")
or -- if you want full flexibility -- even numpy.loadtxt():
for line in f:
a = numpy.loadtxt(StringIO.StringIO(line), dtype=int)
For long lines, these solution will perform better than the Python code in the other answers.
f = open("file", "r")
array = []
line = f.readline()
index = 0
while line:
line = line.strip("\n")
line = line.split()
array.append([])
for item in line:
array[index].append(int(item))
line = f.readline()
index += 1
f.close()
print array