Myfile = open('GG.txt')
c = [i[:] for i in Myfile]
def cole():
x = 0
for line in c:
a = line.strip().split(' ')
for m in a:
if(int(m) > x):
x = int(m)
for line in c:
if str(x) in line:
return ('[' + line.strip() + ']')
print(cole())
下面是我的代码,它获取2D列表并在每一列中查找最大值,然后返回一个包含最大值的列表。
下面是输出:
我的问题是,我如何才能使它用逗号分隔?
所以,期望的输出是:
62,998,4,25,936,126,553,634,316,760,197,181,427,175,259,210
发布于 2017-07-04 02:07:54
在创建c
列表时,请调用strip
、split
和int
,这样就不必在其他循环中重复执行该操作。
您还可以在更新最大值的同时将包含最大值的行保存在一个变量中,因此不需要第二次循环。
没有必要同时使用strip()
和split()
。如果省略了split()
的参数,它将在任何空格上拆分,因此换行符将被忽略。
Myfile = open('GG.txt')
allLines = [[int(num) for num in line.split()] for line in Myfile]
def cole(c):
x = 0
bigLine = []
for line in c:
for m in line:
if (m > x):
x = m
bigLine = line
return bigLine
print(cole(allLines))
https://stackoverflow.com/questions/44895845
复制