How do I turn a string of numbers into a list on Python?
<p>I'm trying to figure out how many numbers in a text file are larger than 0.1.
The text file has 1001 last names and numbers in the following format:</p>

<pre><code>Doe 5
Anderson 0.3
Smith 6
</code></pre>

<p>I figured out how to separate the numbers but i'm having trouble converting my string of numbers into a list so that I can then compare them to 0.1</p>

<p>Here is what I have so far:</p>

<pre><code>    infile = open('last.txt')
    lines = infile.readlines()
    for line in lines:
        items = line.split()
        nums = (items[1])
</code></pre>

<p>also, once I have my list, how do I go about comparing it to 0.1?</p>

<p>The other answers tell you how to count the occurrences which are greater than 0.1, but you may want to have the numbers in a list so that they can be used for other purposes.  To do that, you need a small modification to your code:</p>

<pre><code>with open('last.txt', 'r') as infile:
    lines = infile.readlines()

nums = []
for line in lines:
    items = line.split()
    nums.append(float(items[1]))
</code></pre>

<p>This gives you a list <code>nums</code> of all of the numbers from your file.  Note that I have also used the Python context manager (invoked with <code>with</code>) to open the file which ensures that it is closed properly once you are no longer using it.  </p>

<p>Now you can still count the occurrences of values larger than 0.1 in <code>nums</code>:</p>

<pre><code>sum(1 if x &gt; 0.1 else 0 for x in nums)
</code></pre>

python