I have a code which reads all files in a directory and appends the highest value from each file into a list. The problem is that it does recognise the number 10, but recognises all numbers 0-9. Each file contains the last three scores of each person, from 1-10. However, if the person scores 10, the program does not read that as the highest value, and chooses the second highest from the file and appends that to the list instead. The code works fine if none of their scores are 10. The code should then sort the list according to each person's highest score, which does work, but because it appended the wrong score to the list, therefore it sorts it incorrectly as well.
For example:
[3, 6, 8], highest score is 8, no problem
[6, 10, 9], highest score is 9, why?
The relevant section of code is below. P.s. I have imported all modules and declared all variables at the start (just not visible here), so that is not the problem. Thanks for all help
scores = []
for file in os.listdir(path):
file = os.path.join(path, file)
if os.path.isfile(file):
with open(file, 'r') as txt_file:
scores.append(max(str(n.strip()) for n in txt_file))
results = list(zip(files, scores))
results.sort(key=operator.itemgetter(1), reverse=True)
student_list = [x + ": " + y for x, y in results]
the problem is specifically with this line:
scores.append(max(str(n.strip()) for n in txt_file))
you are grabbing the max str value, and strings compare the same way all other sequences do: compare first element, if they are the same compare next... so when you do:
max("10","9")
it first compares "1" against "9" and sees that "9" is considered greater so that is the string returned, you need to be converting them to ints for them to compare as ints:
scores.append(max(int(n.strip()) for n in txt_file))
# ^ right here
Although since you are opening every single file in a directory if any of the files contain anything other then valid numbers on every line this would fail, so you probably want a try/except, although I cannot give you an example without knowing how files is defined because scores and files need to be the same length.
Related
I am trying to code up a numerical clustering tool. Basically, I have a list (here called 'product') that should be transformed from an ascending list to a list that indicates linkage between numbers in the data set. Reading in the data set, removing carriage returns and hyphens works okay, but manipulating the list based on the data set is giving me a problem.
# opening file and returning raw data
file = input('Data file: ')
with open(file) as t:
nums = t.readlines()
t.close()
print(f'Raw data: {nums}')
# counting pairs in raw data
count = 0
for i in nums:
count += 1
print(f'Count of number pairs: {count}')
# removing carriage returns and hyphens
one = []
for i in nums:
one.append(i.rsplit())
new = []
for i in one:
for a in i:
new.append(a.split('-'))
print(f'Data sets: {new}')
# finding the range of the final list
my_list = []
for i in new:
for e in i:
my_list.append(int(e))
ran = max(my_list) + 1
print(f'Range of final list: {ran}')
# setting up the product list
rcount = count-1
product = list(range(ran))
print(f'Unchanged product: {product}')
for i in product:
for e in range(rcount):
if product[int(new[e][0])] < product[int(new[e][1])]:
product[int(new[e][1])] = product[int(new[e][0])]
else:
product[int(new[e][0])] = product[int(new[e][1])]
print(f'Resulting product: {product}')
I expect the result to be [0, 1, 1, 1, 1, 5, 5, 7, 7, 9, 1, 5, 5], but am met with a 'list index out of range' when using a different data set.
the data set used to give the above desired product is as follows: '1-2\n', '2-3\n', '3-4\n', '5-6\n', '7-8\n', '2-10\n', '11-12\n', '5-12\n', '\n'
However, the biggest issue I am facing is using other data sets. If there is not an additional carriage return, as it turns out, I will have the list index out of range error.
I can't quite figure out what you're actually trying to do here. What does "indicates linkages" mean, and how does the final output do so? Also, can you show an example of a dataset where it actually fails? And provide the actual exception that you get?
Regardless, your code is massively over-complicated, and cleaning it up a little may also fix your index issue. Using nums as from your sample above:
# Drop empty elements, split on hyphen, and convert to integers
pairs = [list(map(int, item.split('-'))) for item in nums if item.strip()]
# You don't need a for loop to count a list
count = len(pairs)
# You can get the maximum element with a nested generator expression
largest = max(item for p in pairs for item in p)
Also, in your final loop you're iterating over product while also modifying it in-place, which tends to not be a good idea. If I had more understanding of what you're trying to achieve I might be able to suggest a better approach.
I have the code below that gets the maximum value from a list. It then compares it to the maximum value of the remaining values in the list, and if it is more than 1 higher than the next greatest value, it replaces the original list maximum with 1 higher than the next greatest value. I would like the code to search the entire list and make sure that any value in the list is at most 1 larger than any other value in the list. I know this ins’t the best worded explanation, I hope the example lists below make what I’m trying to accomplish clearer.
for example I don’t want to get a final list like:
[0,2,0,3]
I would want the final list to be
[0,1,0,2]
input:
empt=[0,2,0,0]
Code:
nwEmpt=[i for i in empt if i !=max(empt)]
nwEmpt2=[]
for i in range(0,len(empt)):
if (empt[i]==max(empt))&(max(empt)>(max(nwEmpt)+1)):
nwEmpt2.append((max(nwEmpt)+1))
elif (empt[i]==max(empt))&(max(empt)==(max(nwEmpt)+1)):
nwEmpt2.append(max(empt))
else:
nwEmpt2.append(empt[i])
output:
nwEmpt2
[0,1,0,0]
min_value = min(empt)
empt_set = set(empt)
for i in empt:
nwEmpt.append(min_value + len(list(filter(lambda x: x < i, empt_set))))
This gives e.g. for input empt = [8, 10, 6, 4, 4] output nwEmpt = [6, 7, 5, 4, 4].
It works by mapping each element to (the minimum value) + (the number of distinct values smaller than element).
Currently working through the springboard data science career track admissions test and one of the questions I got asked was to removes all on non-duplicates from a list of numbers entered via a one line of standard input separated by a space, and return a list of the the duplicates only.
def non_unique_numbers(line):
for i in line:
if line.count(i) < 2:
line.remove(i)
return line
lin = input('go on then')
line = lin.split()
print(non_unique_numbers(line))
The output is inconsistent it seems to remove every other non-duplicate at times but never removes all the non-duplicates, please can you let me know where I am going wrong.
What happens when doing for i in line is that every iteration i gets the value from an iterator created on the variable line. So by changing line you are not changing the iterator.
So, when removing an element at index, say j, all items in index i > j are moved one index down. So now your next item will be again in index j, but the loop will still continue and go to index j+1.
A good way to see this is running your function on an all-non-duplicate values:
>>> l = [0, 1, 2, 3, 4, 5]
>>> print(non_unique_numbers(l))
[1, 3, 5]
You can see that only even-indexed values were removed according to the logic described above.
What you want to do is work on a new, separate list to stack your results. For that you could use simple list comrehension:
lin = input('go on then')
line = lin.split()
print([x for x in line if line.count(x) > 1])
It is not safe to modify a list while iterating through it. The actual problem, I think, is that remove() only removes the first instance of the value, which would make the < 2 check on the last element fail and not call the remove().
Better to use a hash table to find the counts and return those with < 2 then.
a = [2, 5, 6, 12, 21, 25, 32, 41]
This is my list and I want to remove all the numbers which are not in difference of 7.
Before diving into the answers, let's go over what we're dealing with.
You got a list 'A', which you need to loop through to get a list of numbers that are greater than the previous value + 7
If you boil down the question, you get left with two main goals
We need a loop
and we have a list with the final answer
There are two generic ways of approaching this question. In a loop, we populate a new list. The second way is to manipulate the original list.
Although the First approach requires additional memory, I'll be using the First approach for simplicity.
a = [2, 5, 6, 12, 21, 25, 32, 41] # your original list
b = [] # Empty list that will contain final product
for i in range(len(a)):
if len(b) == 0: # if the list is empty, we add first item from 'a' (In our example, it'll be 2)
b.append(a[i])
else:
if a[i] > b[len(b)-1]+7 or a[i] < b[len(b)-1]-7: # for every value of a, we compare the last digit from list b
b.append(a[i])
As far as I have understood your question, in your output list, only those elements should be there whose sum is 7. So that can be achieved by
i=1;
while i<len(a):
if(a[i]-a[i-1] < 7):
a.remove(a[i])
else:
i+=1
print(a)
Basically we are given a list of numbers and we are asked to write an algorithm to find the largest number in the list, note: the numbers are not in order and may contain decimals and negative numbers.
this must be done using loop statements in python 3.2.3
Thanks.
alist=[3,10,90,5,-2,4,18,45,100,1,6]
largest=int()
for large in alist:
if large >= large+1:
largest=large
print(largest)
EDIT
IT WORKS!! Thanks!
alist=[-45,0,3,10,90,5,-2,4,18,45,100,1,-266,706]
largest=alist[0]
for large in alist:
if large > largest:
largest=large
print(largest)
there's also a built in function called max... works like a charm
This question was asked 9 years ago but I'm giving my answer because the question is still relevant today
we can do this for both numbers and strings
A) Finding the largest number in a given list:
your_list = [54, 26, 29, 48, 56, 32, 15, 17]
largest_num = -99999999 # Any value below zero is ok if you know there
# are larger numbers in the list
for i in your_list: # Checking every item in the list
print("current number:", i) # Printing every item in the list
# regardless of their value
if i > largest_num: # Is it larger than -99999999?!
largest_num = i # then value of largest number should be equal
# to the value of i(as integer)
print("largest number:",largest_num) # Let's print the result when
# we're done
B) Finding the largest string in a given list:
my_list = ["a", "b", "c", "A", "B", "C", " "]
largest_str = my_list[0]
for i in my_list:
print("current str:", i)
if i > largest_str:
largest_str = i
print("largest_str:", largest_str)