擅长:python、mysql、java
<p>要为标量处理<code>"character"</code>和<code>"numeric"</code>,请执行以下操作:</p>
<pre><code>def easy_type(ob):
try:
float(ob)
return "numeric"
except ValueError:
return "character"
</code></pre>
<p>要以类似方式处理列表,请假设列表的所有元素都是同一类型的,并且列表不是嵌套的,也不是空的:</p>
<pre><code>def easy_type(ob):
if isinstance(ob, list):
return mytype(ob[0])
try:
float(ob)
return "numeric"
except ValueError:
return "character"
</code></pre>
<p>也要处理<code>"multiple"</code>:</p>
<pre><code>def easy_type(ob):
if isinstance(ob, list):
types = set((mytype(o) for o in ob))
if len(types) > 1:
return "multiple"
else:
return types.pop()
try:
float(ob)
return "numeric"
except ValueError:
return "character"
</code></pre>