我的代码可以计算提供的文件 alpha.txt 中每一行的最小/最大/总和,但它不能按列计算最小/最大/总和....有关如何做到这一点的任何想法都会有所帮助,谢谢!
def load_data():
usrnput = input("Enter a filename ")
my_list = [[float(i) for i in line.split(',')] for line in open(usrnput, "+r")]
row = len(my_list)
column = len(my_list[0])
if row != column:
print("invalid")
else:
pass
count = 0
for row in(my_list):
count = count + 1
print(count, row)
secondc = input("Enter a number ")
if secondc == '2':
minimum(my_list)
elif secondc =='3':
maximum(my_list)
elif secondc =='4':
sum1(my_list)
def minimum(my_list):
pickaposition = input("Enter a row or column: ")
if pickaposition == ('1'):
print(min(my_list[0]))
elif pickaposition == ('2'):
print(min(my_list[1]))
elif pickaposition == ('3'):
print(min(my_list[2]))
elif pickaposition == ('4'):
print(min(my_list[3]))
elif pickaposition == ('5'):
print(min(my_list[4]))
def maximum(my_list):
pickaposition = input("Enter a row or column: ")
if pickaposition == ('1'):
print(max(my_list[0]))
elif pickaposition == ('2'):
print(max(my_list[1]))
elif pickaposition == ('3'):
print(max(my_list[2]))
elif pickaposition == ('4'):
print(max(my_list[3]))
elif pickaposition == ('5'):
print(max(my_list[4]))
def sum1(my_list):
pickaposition = input("Enter a row or column: ")
if pickaposition == ('1'):
print(sum(my_list[0]))
elif pickaposition == ('2'):
print(sum(my_list[1]))
elif pickaposition == ('3'):
print(sum(my_list[2]))
elif pickaposition == ('4'):
print(sum(my_list[3]))
elif pickaposition == ('5'):
print(sum(my_list[4]))
def main():
print("""1 - Open and load from a file
2 - Minimum
3 - Maximum
4 - Sum
5 - Delete
6 - Save
7 - Save as (specify new file name)
0 - Exit
""")
pick = input("Enter a number ")
if pick == "1":
load_data()
else:
pass
main()
alpha.txt 也包含数据
5,4,2,3.2
1,.2,4.4,8
3,8,6.5,2
3,2,1,5.3
我真的需要根据列分配变量 AZ,但我也不知道该怎么做。
这是我的代码的完整示例运行,可能会让您更轻松。
1 - Open and load from a file
2 - Minimum
3 - Maximum
4 - Sum
5 - Delete
6 - Save
7 - Save as (specify new file name)
0 - Exit
Enter a number 1
Enter a filename alpha.txt
1 [5.0, 4.0, 2.0, 3.2]
2 [1.0, 0.2, 4.4, 8.0]
3 [3.0, 8.0, 6.5, 2.0]
4 [3.0, 2.0, 1.0, 5.3]
Enter a number 3
Enter a row or column: 3
8.0
>>>