Find the max sum for each line and find max list and line number of maximum list count in python
Question: 
    "[1.0, 0.5]","[0.5, 0.5, 0.5]","[0.5, 0.5]"
    
    "[1.0, 0.0]","[0.5, 0.5, 0.5]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[0.0, 0.0, 0.0]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[1.0, 1.0, 1.0]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[0.5, 0.5, 0.5]","[1.0, 1.0]"
    
    "[0.0, 0.0]","[0.0, 0.0, 1.0]","[0.5, 0.5]"
    

"[1.0, 0.5]","[0.5, 0.5, 0.5]","[0.5, 0.5]"

    
    
    "[1.0, 0.0]","[0.5, 0.5, 0.5]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[0.0, 0.0, 0.0]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[1.0, 1.0, 1.0]","[0.0, 0.0]"
    
    "[0.0, 0.0]","[0.5, 0.5, 0.5]","[1.0, 1.0]"
    
    "[0.0, 0.0]","[0.0, 0.0, 1.0]","[0.5, 0.5]"
    

I have list of diagonals now i want to find the sum for each list in each
line. Output should be like this

    
    
    1.5
    
    1.5
    
    0
    
    3
    
    1.5
    
    1
    

How can i do this?


Answer: 
    from ast import literal_eval
    with open("in.csv") as f:
        for ind, line in enumerate(f, 1):
            if line.strip():  # catch empty lines
                print(ind,max(sum(literal_eval(sub.strip('"'))) for sub in line.rstrip().split('",')))
    

Output:

    
    
    (1, 1.5)
    (2, 1.5)
    (3, 0.0)
    (4, 3.0)
    (5, 2.0)
    (6, 1.0)
    

Using the csv module makes life easier as it does the splitting correctly for
us:

    
    
    import csv
    from ast import literal_eval
    with open("in.csv") as f:
        reader = csv.reader(f)
        for ind, row in enumerate(reader,1):
            if row: # only needed if you have empty rows
                print(ind, max(sum(literal_eval(sub)) for sub in row))
    

Output:

    
    
    (1, 1.5)
    (2, 1.5)
    (3, 0.0)
    (4, 3.0)
    (5, 2.0)
    (6, 1.0)
    

[ast.literal_eval](https://docs.python.org/2/library/ast.html#ast.literal_eval)

> Safely evaluate an expression node or a Unicode or Latin-1 encoded string
> containing a Python literal or container display. The string or node
> provided may only consist of the following Python literal structures:
> strings, numbers, tuples, lists, dicts, booleans, and None.
>
> This can be used for safely evaluating strings containing Python values from
> untrusted sources without the need to parse the values oneself. It is not
> capable of evaluating arbitrarily complex expressions, for example involving
> operators or indexing.

Considering you have a csv file then it is a no brainer which option to use.

If you want the max of all the lines, we can use a generator expression to get
the max from each line then use operator.itemgetter to get the max based on
the second element of our tuple which is the max:

    
    
    import csv
    from ast import literal_eval
    from operator import itemgetter
    with open("in.csv") as f:
        reader = csv.reader(f)
        totals = ((ind, max(sum(literal_eval(sub))for sub in row))
                  for ind, row in enumerate(reader, 1) if row)
        print(max(totals,key=itemgetter(1)))
    

Output:

    
    
     (4, 3.0) # line 4 
    

