Home > front end >  I want add my simples list in list of comprehension in python
I want add my simples list in list of comprehension in python

Time:12-20

new_q #list q

#output [6.0, 5.75, 5.5, 5.25, 5.0, 4.75, 4.5, 4.25, 4.0, 3.75, 3.5, 3.25, 3.0, 2.75, 2.5, 2.25, 2.0, 1.75, 1.5, 1.25, 1.0, 0.75, 0.5, 0.25]

t # list of time

#output time [0.38, 0.51, 0.71, 1.09, 2.0, 5.68, 0.31, 0.32, 0.34, 0.35, 0.36, 0.38, 0.4, 0.42, 0.44, 0.48, 0.51, 0.56, 0.63, 0.74, 1.41, 2.17, 3.97, 11.36]

i want to put this to list in a comprehension list like 24x2 matrix.

one of columns is my q and the other is t and q[0] matches with t[0].

what i need to do?

CodePudding user response:

It seems that you just need to zip your two lists:

myList = [0,1,2,3,4,5]
myOtherList = ["a","b","c","d","e","f"]

# Iterator of tuples
zip(myList, myOtherList)

# List of tuples
list(zip(myList, myOtherList))

You will get this result: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f')].

If you need another structure, you could use comprehension:

length = min(len(myList), len(myOtherList))

# List of list
[[myList[i], myOtherList[i]] for i in range(length)] 

# Dict
{myList[i]: myOtherList[i] for i in range(length)}

CodePudding user response:

You can use zip() function like this

q = [6.0, 5.75, 5.5, 5.25, 5.0, 4.75, 4.5, 4.25, 4.0, 3.75, 3.5, 3.25, 3.0, 2.75, 2.5, 2.25, 2.0, 1.75, 1.5, 1.25, 1.0, 0.75, 0.5, 0.25]
t = [0.38, 0.51, 0.71, 1.09, 2.0, 5.68, 0.31, 0.32, 0.34, 0.35, 0.36, 0.38, 0.4, 0.42, 0.44, 0.48, 0.51, 0.56, 0.63, 0.74, 1.41, 2.17, 3.97, 11.36]

L1 = list(zip(q, t))

res = []

for i, j in L1:
    res.append(i)
    res.append(j)

print(res)
  • Related