How to work with items in a list?

I am making a python program that should contain some information about lists, and they perform mathematical operations on them. Here is an example 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 its meanings with something else. here is another example 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 this is causing me a mistake, 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 
+5
source share
2 answers

The problem is that you use array.extend() when you want array.append() . .extend takes an .extend and unpacks its contents and adds it to the end of the list. .append takes a value and adds it to the end of the list without unpacking its contents. Since you want to add a tuple ( (temp,fecha) ) to the list (and not every element in the tuple), you should use array.append() .

EDIT

All that said, there are many places to improve your code. I simplified all the code you posted and got it up to 7 lines. (It should work just like your code, but not promises, since I have not seen your entire 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!

+1
source

The problem is the misuse of the extension function. Therefore, when you execute I [0] in the second function, it will be an error, since this is not a list, but a number.

You should check the function append and extend.

0
source

All Articles