私のコードは、提供されたファイル 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
>>>