Home > front end >  How can i do a list from list?
How can i do a list from list?

Time:03-06

I have this list:

l1 = [[-5, 10]] [[-3, 5]] [[-53.7228, 3.72281]] [[-3, 4]] [[-6.32456, 6.32456]]

how can i get this?

[[-5, 10], [-3, 5], [-53.7228, 3.72281], [-3, 4], [-6.32456, 6.32456]]

i tried this but it doesn't work

l=[]
for i in l1:
    l.append(i)
    print(l)

i have this code and i got that output:

lines = [[-5, -50], [-2, -15], [50, -200], [-1, -12], [0, -40]]
for m in lines:
    m1 = m[0]
    m2 = m[1]

def solver(m1, m2):
    from math import sqrt
    D = m1*m1 - 4*m2# обчислення дискримінанта
    if D >= 0:
        x1 = (-m1   sqrt(D)) / 2
        x2 = (-m1 - sqrt(D)) / 2
        l1=[]
        if m1 >= 0 and m2 >= 0:
            text = "нерівність : "   'x^2 '   str(m1)   '*x '   str(m2)   "<0  її розв'язок = "   str([round(x2, 5), round(x1, 5)])
            # print(text)
            l1.append([round(x2, 5), round(x1, 5)])
        elif m1 >= 0 and m2 <= 0:
            text = "нерівність : "   'x^2 '   str(m1)   '*x'   str(m2)   "<0  її розв'язок = "   str([round(x2, 5), round(x1, 5)])
            # print(text)
            l1.append([round(x2, 5), round(x1, 5)])
        elif m1 <= 0 and m2 <= 0:
            text = "нерівність : "   'x^2'   str(m1)   '*x'   str(m2)   "<0  її розв'язок = "   str([round(x2, 5), round(x1, 5)])
            # print(text)
            l1.append([round(x2, 5), round(x1, 5)])
        elif m1 <= 0 and m2 >= 0:
            text = "нерівність : "   'x^2'   str(m1)   '*x '   str(m2)   "<0  її розв'язок = "   str([round(x2, 5), round(x1, 5)])
            # print(text)
            l1.append([round(x2, 5), round(x1, 5)])
        print(l1, end=' ')
    else:
        pass
c = solver(m1, m2)

CodePudding user response:

I am assuming the given list is

l1 = [[-5, 10]], [[-3, 5]], [[-53.7228, 3.72281]], [[-3, 4]], [[-6.32456, 6.32456]]

Your code would work if you make this change

l=[]
for i in l1:
    l.append(i[0]) # accessing the first element then appending
    print(l)

CodePudding user response:

You can also use a tuble inside your list.

l1 = [(-5, 10), (-3, 5), (-53.7228, 3.72281), (-3, 4), (-6.32456, 6.32456)]
  • Related