1

I have a list of elements to which I inputted some "identifiable" values that are not to go to my database. I want to find and substitute those values. Code looks like this (tried to be generic and illustrative, the date and time is predefined vars):

A = []
A.append(['Name1',date1,time1,0.00])
A.append(['Name1',date1,time2,0.00])
A.append(['Name2',date1,time1,price1])
A.append(['Name1',date1,time3,price2])
A.append(['Name1',date1,time4,price3])
A.append(['Name1',date2,time5,price4])

and so on. This 0.00 price should be changed by the next price where we have 'Name1' in position 0 and date1 in position 1, i.e.:

print(A[0])
print(A[1])

should yield

['Name1',date1,time1,price1]
['Name1',date1,time2,price1] 

Appreciate your help.

liushuaikobe
  • 2,152
  • 1
  • 23
  • 26

3 Answers3

2

Try this code for printing, pass the list and index for the same.

def print1(lists, index):
    if lists[index][3] == 0:
            try:
                    name=lists[index][0]
                    val = next(l[3] for l in lists[index:] if l[3]>0 and l[0]==name)
                    print lists[index][:-1] + [val]
            except:
                    print "No value found with same name where price>0"
    else:
            print lists[index]

A=[]
A.append(['Name1','date1','time1',0.00])
A.append(['Name1','date1','time2',0.00])
A.append(['Name2','date1','time1',10])
A.append(['Name1','date1','time3',20])
A.append(['Name1','date1','time4',30])
A.append(['Name1','date2','time5',40])
print1(A,1)

you can return the values in place of printing them in case you need them to.

Namit Singal
  • 1,506
  • 12
  • 26
  • Thanks Namit. that was very helpful and should work within the framework I'm programming – Eduardo Hinterholz Jun 06 '15 at 03:00
  • @NamitSingal that's great! This replaces the `0.00` in `(A,1)` I am wondering how to replace all the `0.00` in A based on the same logic if A is a bigger list with similar data. Actually, even in this A it only replaces `0.00` in `(A,1)` not in '(A,0)`. How would you replace in both `(A,1)` and '(A,0)`? – Joe T. Boka Jun 06 '15 at 04:16
  • @JoeR that would be a different scenario, you could use a combination of map and tree to accomplish the same, on first pass store 'Name' as the key, and have a tree with indexes so that we do not replace the value of 'Name' with the previous indices. First pass will populate this DS, second pass we will just keep on filling the values. There can be simpler solution if the exact context of the problem is known. – Namit Singal Jun 06 '15 at 12:46
  • @NamitSingal thanks for the answer. I actually thought that's what the OP was trying to accomplish. In any case, good answer! – Joe T. Boka Jun 06 '15 at 13:14
0

May be you need a method like this:

A = []
def insert_or_update_by_name_and_date(x):
    if not isinstance(x, list) or len(x) < 2:
        return
    for element in A:
        if len(element) < 2:
            continue
        if element[0] == x[0] and element[1] == x[1]:
            element[2] = x[2]
            element[3] = x[3]
            return
    A.append(x)
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
0

You can use two for loops:

for i in range(len(A)):
    for j in range(i, len(A)):
        if A[i][3] == 0.0 and A[j]]0] == 'Name1' and A[j][1] == date1:
            A[i][3] = A[j][3]

Apart from that, you may find this discussion on when to use a Dictionary, List or Set useful.

Community
  • 1
  • 1