本帖最后由 Jorin 于 2016-3-22 15:32 编辑
在rhinopython中分开一个列表中的数字和字符串是非常的简单,直接判断下就行了。这里不多说了,直接上代码。
- #coding=utf-8
- from types import StringType
- a = [1,2.3,"h","j",3.0,"j"]
- b=[];c=[]
- for i in a:
- if (type(i) == StringType):
- b.append(i)
- else:
- c.append(i)
- print b
- print c
复制代码
然而在ghpython中输入的时候都会被默认转换成字符串,这样分开就比较麻烦。这里提供了一种gh的方法和ghpython的方法。gh主要利用强行转换类型办到的,然而在gh中字符串“e”会被当成自然常数e,所以需要再做个判断。
在ghpython中主要利用的错误处理机制来实现的,也就是try..except...else结构办到的,详细请查看代码。
- a=[];b=[]
- for i in x:
- try:
- float(i)
- except ValueError:
- a.append(i)
- else:
- if i.find(".") != 1:
- b.append(int(i))
- else:
- b.append(float(i))
复制代码
源文件下载链接:http://pan.baidu.com/s/1i3pixNn 密码:prfs[/hide]
|