下面是代码:
如何简化代码的技巧是受欢迎的,
eqlCounter = 0
octals = []
with open("D:\matura\Matura2017\Dane_PR2\liczby.txt", "r") as f: #file contains 2 sets of numbers(1st decimal, second octal)
for x in f:
lines = f.readline()
splited = lines.split()
toInt = int(splited[1], 8) #oct to int(dec)
octals.append(toInt)
if int(splited[0]) == toInt:
eqlCounter += 1
print("same nmbrs: ", eqlCounter) #a
print("min: ", min(octals),"at: ")
print("max: ", max(octals),"at: ")
我想找一个包含最大值(已经找到那个值)的行的位置,我知道我可以用另一个循环来找到位置,但是我想知道是否有一个更短、更有效的方法来实现它
发布于 2021-02-03 21:51:27
有一种方法可以在可迭代的index
中找到值的位置。
low = min(octals)
print("min: ", low ,"at: ", octals.index(low))
另一种可能是在迭代文件时跟踪极值和它们的位置。例如:
low = 99999999999999999999999999999999999999999999999999
with ...
for idx, x in enumerate(f):
if toInt < low:
low = toInt
low_pos = idx
同样对待高元素。
https://stackoverflow.com/questions/66036209
复制相似问题