how to convert type of variables in python?
<p>Suppose</p>

<pre><code>    type(a) = &lt;type 'numpy.ndarray'&gt;
</code></pre>

<p>now I have another variable <code>b</code> (maybe a <code>&lt;type 'list'&gt;</code>), I want to convert it into the same type of <code>a</code>. How to do this? For example, </p>

<pre><code>    &gt;&gt;&gt; type(a)
    &lt;type 'numpy.ndarray'&gt;
    &gt;&gt;&gt; b = [[[100, 200, 300]]]
    &gt;&gt;&gt; type(b)
    &lt;type 'list'&gt;
</code></pre>

<p>Now, I want to </p>

<pre><code>    &gt;&gt;&gt; type(b)
    &lt;type 'numpy.ndarray'&gt;
</code></pre>

<p>I want to do this in a python script. I don't know in advance what is the type of <code>a</code>. I just take <code>numpy.ndarray</code> as an example.</p>

<p><strong>UPDATE:</strong></p>

<p>In fact, in the python script, it receive some strings from <code>sys.argv</code>. And then it will assign these strings into some predefined variables in this script. These predefined variables may be <code>int</code>, <code>list</code>, <code>numpy.ndarray</code>, or else. For <code>int</code> or <code>list</code>, this can be done by <code>from ast import literal_eval; MyDict[sys.argv[1]] = literal_eval(sys.argv[2])</code>. But if <code>MyDict[sys.argv[1]]</code> is a <code>numpy.ndarray</code>, the <code>literal_eval</code> cann't reassign the list <code>[[[100, 200, 300]]]</code> into a <code>numpy.ndarray</code>. So I'm seeking a more effective way to do this.</p>

<p>To convert a <code>list</code> into a python array, use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html" rel="nofollow"><code>numpy.asarray</code></a>.
For example:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a = [1, 2, 3, 4]
&gt;&gt;&gt; a = np.asarray(a)
&gt;&gt;&gt; a
array([1, 2, 3, 4])
&gt;&gt;&gt; type(a)
&lt;class 'numpy.ndarray'&gt;
</code></pre>

<p>More generally, how to convert the type depends on what conversion you want.
To convert to a string use <a href="https://docs.python.org/3/library/stdtypes.html#textseq" rel="nofollow"><code>str</code></a>, to convert to an integer use <code>int</code>, and so on.
Note that in python the variables to not have a fixed "type", so you can simply reassign a variable to convert its type. For example</p>

<pre><code>&gt;&gt;&gt; a = '2' # we can start from a string..
&gt;&gt;&gt; a = int(a) # and than get an int
&gt;&gt;&gt; a
2
&gt;&gt;&gt; a = [1, 2, 3] # we can now completely change and make *a* a list
&gt;&gt;&gt; a = tuple(a) # or maybe you want a tuple?
&gt;&gt;&gt; a
(1, 2, 3)
</code></pre>

<p>To check if a variable is of a certain type you can use <code>isinstance</code>, so to convert the type of <code>b</code> depending on the type of <code>a</code> you can do something like</p>

<pre><code>def conditional_conversion(a, b):
    if isinstance(a, int):
        b = int(b)
    elif isinstance(a, str):
        b = str(b)
    elif isinstance(a, list):
        b = list(b)
    return b

&gt;&gt;&gt; a = 12
&gt;&gt;&gt; b = '51'
&gt;&gt;&gt; b = conditional_conversion(a, b)
51
</code></pre>

<p>Of course, you'd need to consider the various possibilities that you could incur into.
Also, this type of conversion will likely throw exception if <code>b</code> cannot be converted into the type of <code>a</code>, so a try-catch construct will probably be beneficial.</p>

python