简体   繁体   中英

Make list of X and Y coordinates

I have the following list:

Filedata:
[1, 0, 0, 0]
[2, 0, 0, 100]
[3, 0, 0, 200]
[4, 100, 0, 0]
[5, 100, 0, 100]
[6, 100, 0, 200]
...

where the first column is an ID, the second is the X coordinate, the third the Y coordinate and the fourth the Z coordinate.

I'd like to make two lists of the different X and Y coordinates in the original list:

X:
[0, 100, ...]
Y:
[0, 100, ...]

to do this I thought of a code but in the process I realised there must be an easier why of doing this instead of doing multiple for and if loops. Any ideas?

IDs,Xs,Ys,Zs = zip(*filedata)
positions = zip(Xs,Ys,Zs) # list of tuples of (x,y,z) 

unique_Xs = []
[unique_Xs.append(val) for val in Xs if val not in unique_Xs]

unique_Ys = []
[unique_Ys.append(val) for val in Ys if val not in unique_Ys]

I think would work , assuming filedata is a 2d list, although filedata is not a great variable name ...

I added a way to get unique points in each list ... not entirely sure thats what you want though

filedata = [
    [1, 0, 0, 0],
    [2, 0, 0, 100],
    [3, 0, 0, 200],
    [4, 100, 0, 0],
    [5, 100, 0, 100],
    [6, 100, 0, 200],
]

IDs, Xs, Ys, Zs = zip(*filedata)
Xs, Ys, Zs = set(Xs), set(Ys), set(Zs)
print 'X:', Xs
print 'Y:', Ys
print 'Z:', Zs

It seems OP only wants lists of X's and Y's. He can do something like this:

xs=zip(*filedata)[1]
ys=zip(*filedata)[2]

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