简体   繁体   中英

Create list from two tuples of tuples

I have two tuples of tuples:

dCF3t=((((1.90683376789093, -44705.1875), (1.90689635276794, -44706.76171875)),),)
dU1t=((((0.0, 0.00244894321076572), (0.00249999994412065, 0.00782267469912767)),),)

I need to create a list with the value of the second column of each tuple:

dFD=[]
dFD.append([x[1] for x in dU1t, y[1] for y in dCF3t])

Example:

dFD=[[0.00244894321076572,-44705.1875],[0.00782267469912767,-44706.76171875]]

But it gives me this error: name 'y' is not defined

PS: Both tuples were created from lists of lists of tuples.

EDIT: To avoid the ,),) at the end of the tuples please consider:

dCF3t=[[((1.90683376789093, -44705.1875), (1.90689635276794, -44706.76171875))]]
dU1t=[[((0.0, 0.00244894321076572), (0.00249999994412065, 0.00782267469912767))]]

SOLUTION:

    dFD=[]
    for i in range(0, len(dU1t[0][0])):
        dFD.append([dU1[0][0][i][1],dCF3[0][0][i][1]])

I believe you're looking for

dFD=[[x[1],y[1]] for x,y in zip(dU1t[0], dCF3t[0])]

In general, you should try to avoid appending things as much as possible; it typically slows down because it can require copying the entire list to a new location in memory. In your example, the append statement is essentially trivial, so it wouldn't really cost you much, but it's also completely unnecessary.

I suspect you want something like this:

dFD.append([x[1] for x in dU1t]+[y[1] for y in dCF3t])

Although, you might want to use extend rather than append unless you want dFD to contain a list within a list.

这似乎与您的示例相符:

dFD = [[x[1], y[1]] for x,y in zip(dU1t[0], dCF3t[0])]

Alright, there are several problems here:

  1. Your "tuples of tuples" are actually tuples of tuples of tuples of tuples. The layering is rather obscure, but essentially the tuples of tuples are encapsulated in two layers of one-element tuples. For the rest of this post, I will assume that the variables are set as follows instead:

     dCF3t=(1.90683376789093, -44705.1875), (1.90689635276794, -44706.76171875) dU1t=(0.0, 0.00244894321076572), (0.00249999994412065, 0.00782267469912767) 

    If it's not possible to generate them in this form, try getting rid of the outer tuple with

     dCF3t = dCF3t[0][0] dU1t = dU1t[0][0] 
  2. You should use dFD.extend() instead of dFD.append() . Extend adds all elements of an iterable passed to it, while append simply adds it's argument to the list. That said, it isn't necessary to use either, as the list is empty at the start. You could simple set it directly to the list comprehension.

  3. The list comprehension itself is the part which actually errored. The problem is that you are trying to iterate over two different objects in the same list comprehension; for your purposes python's zip method is ideal here:

     dFD = [(x[1], y[1]) for x, y in zip(dU1t, dCF3t)] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM