How to convert some string fields in list to integer in Python?
<p>I have a string list and want to convert last value to integer, </p>

<pre><code>[['wind turbine', 'feed induction generator', '16'],
['wind turbine', 'dynamic model', '6'],
['electrical model', 'dynamic model', '4']]
</code></pre>

<p>I want just convert last fields to integer. </p>

<p>results something like this:</p>

<pre><code>[['wind turbine', 'feed induction generator', 16  ],
['wind turbine', 'dynamic model', 6  ],
['electrical model', 'dynamic model', 4  ]]
</code></pre>

<p>Update : Added support for float numbers.  </p>

<p>We can use <code>isdigit()</code> for positive int and <code>str(x).startswith('-') and x[1:].isdigit()</code> for negative int as well as <code>isFloat(aNumber)</code> for float numbers. </p>

<p>Below code works with both positive and negative int.</p>

<pre><code>myList = [['wind turbine', 'feed induction generator', '16'], 
['wind turbine', 'dynamic model', '6'],
['electrical model', 'dynamic model', '4']]   

 def isFloat(aNumber):        
     try:
        float(aNumber)
        return True
     except:
        return False

finalList = []
for subList in myList:
    aList = []
    for x in subList:               
        if x.isdigit():
            aList.append(int(x))
        elif str(x).startswith('-') and x[1:].isdigit():
            aList.append(int(x))
        elif isFloat(x):
            aList.append(float(x))            
        else:
            aList.append(x)

    finalList.append(aList)


print finalList
</code></pre>

<p><strong>Output:</strong></p>

<blockquote>
  <p>Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
  Type "copyright", "credits" or "license()" for more information.</p>
  
  <blockquote>
    <blockquote>
      <p>=================================================</p>
      
      <p>[['wind turbine', 'feed induction generator', 16], ['wind turbine', 'dynamic model', 6], ['electrical model', 'dynamic model', 4]]</p>
    </blockquote>
  </blockquote>
</blockquote>

python