[英]How to operate on elements on a list?
I'm making a program on python that needs to hold some info on lists and them execute mathematical operations on them. 我在python上创建一个程序,需要在列表上保存一些信息,然后对它们执行数学运算。 Here's a sample of my code: 这是我的代码示例:
VCentral = []
Atlantico=[]
Pacifico=[]
Norte=[]
Sur=[]
LVC=0
LA=0
LP=0
LN=0
LS=0
LTotal=0
def RegTemp(regcode):
global LVC
global LA
global LP
global LN
global LS
global LTotal
registro=[]
temp = int(input("Digite la temperatura: "))
fecha=input("Digite la fecha: ")
registro.extend((temp,fecha))
if regcode==1:
VCentral.extend(registro)
LVC+=1
LTotal+=1
if regcode==2:
Atlantico.extend(registro)
LA+=1
LTotal+=1
if regcode==3:
Pacifico.extend(registro)
LP+=1
LTotal+=1
if regcode==4:
Norte.extend(registro)
LN+=1
LTotal+=1
if regcode==5:
Sur.extend(registro)
LS+=1
LTotal+=1
And then I need to compare it's values to something else. 然后我需要将它的值与其他东西进行比较。 here's another sample of the function I'm trying to implement: 这是我正在尝试实现的函数的另一个示例:
def Mayor(regcode):
if regcode==1:
may=0
for i in VCentral:
if i[0]>may:
may=i[0]
return may
if regcode==2:
may=0
for i in Atlantico:
if i[0]>may:
may=i[0]
return may
if regcode==3:
may=0
for i in Pacifico:
if i[0]>may:
may=i[0]
return may
if regcode==4:
may=0
for i in Norte:
if i[0]>may:
may=i[0]
return may
if regcode==5:
may=0
for i in Sur:
if i[0]>may:
may=i[0]
return may
If you could tell me why it throws an error at me I would appreciate it. 如果你能告诉我为什么它会给我一个错误,我会很感激。
EDIT: 编辑:
Traceback (most recent call last):
File "D:/tarea2.py", line 212, in <module>
Menu()
File "D:/tarea2.py", line 199, in Menu
print(EstadisticaZona(regcode))
File "D:/tarea2.py", line 165, in EstadisticaZona
print("Temperatura mayor: ",Mayor(2))
File "D:/tarea2.py", line 102, in Mayor
if i[0]>may:
TypeError: 'int' object is not subscriptable
The problem is that you are using array.extend()
when you want array.append()
. 问题是,你正在使用array.extend()
当你想array.append()
.extend
takes an iterable and unpacks its contents and adds that to end of the list. .extend
接受一个iterable并解压缩其内容并将其添加到列表的末尾。 .append
takes a value and adds it to the end of the list without unpacking its contents. .append
接受一个值并将其添加到列表的末尾,而不解压缩其内容。 Since you want to add a tuple ( (temp,fecha)
) to the list (and not each item in the tuple), you should use array.append()
. 由于您要将一个元组( (temp,fecha)
)添加到列表中(而不是元组中的每个项目),您应该使用array.append()
。
EDIT 编辑
All that being said, there are a lot of places for improvement in your code. 总而言之,您的代码中有很多需要改进的地方。 I simplified all the code you posted quite a bit and got it down to 7 lines. 我简化了你发布的所有代码并将其归结为7行。 (It should work the same as your code, but no promises as I haven't seen your whole program.): (它应该与您的代码一样工作,但没有承诺,因为我没有看到您的整个程序。):
oceans = [[], [], [], [], []]
def RegTemp(regcode):
temp = int(input("Digite la temperatura: "))
fecha = input("Digite la fecha: ")
oceans[regcode-1].append((temp,fecha))
def Mayor(regcode):
return max(i[0] for i in oceans[regcode-1])
Good luck and happy coding! 祝你好运,编码愉快!
The problem is you misuse the extend function. 问题是你滥用了扩展功能。 So when you do i[0] in the second function, it will be an error, since it is not list but a number. 所以当你在第二个函数中执行i [0]时,它将是一个错误,因为它不是列表而是数字。
You should check the append and extend function. 你应该检查追加和扩展功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.