Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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
like image 717
Dave Carballo Avatar asked Nov 10 '22 05:11

Dave Carballo


1 Answers

The problem is that you are using array.extend() when you want array.append(). .extend takes an iterable and unpacks its contents and adds that to 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 each item in the tuple), you should use 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. (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!

like image 192
pzp Avatar answered Dec 05 '22 20:12

pzp