python拆分字符串生成列表_Python - 包含字符串和整数的拆分列表

myList = [ 4,'a', 'b', 'c', 1 'd', 3]

how to split this list into two list that one contains strings and other contains integers in elegant/pythonic way?

output:

myStrList = [ 'a', 'b', 'c', 'd' ]

myIntList = [ 4, 1, 3 ]

NOTE: didn't implemented such a list, just thought about how to find an elegant answer (is there any?) to such a problem.

解决方案

As others have mentioned in the comments, you should really start thinking about how you can get rid of the list which holds in-homogeneous data in the first place. However, if that really can't be done, I'd use a defaultdict:

from collections import defaultdict

d = defaultdict(list)

for x in myList:

d[type(x)].append(x)

print d[int]

print d[str]