0

I'm somewhat new to python (switching from IDL), so I apologize if I'm not using the right terminology. I've tried looking for similar questions, but can't seem to figure it out. I have two lists and I'm trying to create a histogram of the data where dat2 = 1. I've tried doing this multiple ways and it keeps giving me a TypeError

import matplotlib.pyplot as plt
import numpy as np
data = [1.1,4.2,5.3,8.6,10.0,1.2,41.4,23.2]
dat2 = [1,1,1,1,2,2,2,2]
ind = [i for i,v in enumerate(dat2) if v==1]
bins = np.arange(0,45,5)
plt.hist(data[ind],bins)

The error points to the hist() line and says "TypeError: list indices must be integers, not list." I've tried ind=map(int,ind) and ind=[int(i) for i in ind] with no luck.

Community
  • 1
  • 1
edub
  • 659
  • 4
  • 8
  • 19
  • What you are trying to do is possible with NumPy arrays, so better initialize `data` and `dat2` as NumPy arrays. – Ashwini Chaudhary Feb 08 '15 at 22:05
  • I'm not sure how to do that if I'm reading the data from a text file? – edub Feb 08 '15 at 22:08
  • NumPy provides functions like [`loadtxt`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html) and [`genfromtxt`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt) to load data from files to NumPy arrays, if they are not good enough then you can convert your lists to NumPy arrays by calling [`array()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html) on them. – Ashwini Chaudhary Feb 08 '15 at 22:11

2 Answers2

0
data = [ d1 for (d1, d2) in zip(data, dat2) if d2 == 1 ]
plt.hist(data)

This zips data and dat2 together, creating a list of tuples [ (1.1, 1), (4.2, 1) ... ]. You can then use a list comprehension to keep only those tuples whose second element is 1.

Finally, should the lists be long enough for memory to be an issue, you can replace zip with itertools.izip, which returns an iterator for the zipped lists, rather than constructing them explicitly.

Pascal Bugnion
  • 4,878
  • 1
  • 24
  • 29
0

When you'r doing math, you typically use the numpy package's ndarray object, which specifically allows this kind of indexing:

data = np.array(data)
...
data[ind]
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94