我有一个具有以下外观的列表:
[0] = [ [0.0, 100.0], [0.1, 93.08], [0.3, 92.85], [0.5, 92.62], [0.7, 91.12], [0.9, 90.89] ] [1] = [ [0.0, 100.0], [0.1, 2.79], [0.3, 2.62], [0.5, 2.21], [0.7, 1.83], [0.9, 1.83] ]
我想获取矢量来绘制信息如下:
[0.0, 0.1, 0.3, 0.5, 0.7, 0.9] [100.0, 93.08, 92.85, 92.62, 91.12, 90.89]
与列表中的所有条目相同.我在尝试这样的事情:
x = mylist[0][:][0]
有任何想法吗?我很感激帮助!
用途zip
:
>>> mylist = [ [0.0, 100.0], [0.1, 93.08], [0.3, 92.85], [0.5, 92.62], [0.7, 91.12], [0.9, 90.89] ] >>> a, b = zip(*mylist) >>> a (0.0, 0.1, 0.3, 0.5, 0.7, 0.9) >>> b (100.0, 93.08, 92.85, 92.62, 91.12, 90.89) >>> list(a) [0.0, 0.1, 0.3, 0.5, 0.7, 0.9] >>> list(b) [100.0, 93.08, 92.85, 92.62, 91.12, 90.89]
使用pure-python,你应该使用list-comprehension
data = [ [0.0, 100.0], [0.1, 93.08], [0.3, 92.85], [0.5, 92.62], [0.7, 91.12], [0.9, 90.89] ] listx = [item[0] for item in data ] listy = [item[1] for item in data ] >>>listx [0.0, 0.1, 0.3, 0.5, 0.7, 0.9] >>>listy [100.0, 93.08, 92.85, 92.62, 91.12, 90.89]
我认为它比zip更好一点,因为它更容易阅读,你不必抛出元组