string과 int/float을 단일 배열에서 분리하는 데 문제가 있습니다.목록에서 데이터 유형을 분리하는 방법은 무엇입니까?
이a = [1,2,3,'a',4,11,212,'a','b','c',3,32,4,'d','e']
원하는 출력 :
['a','a','b','c','d','e']
string과 int/float을 단일 배열에서 분리하는 데 문제가 있습니다.목록에서 데이터 유형을 분리하는 방법은 무엇입니까?
이a = [1,2,3,'a',4,11,212,'a','b','c',3,32,4,'d','e']
원하는 출력 :
['a','a','b','c','d','e']
파이썬 오브젝트가 지정된 형태의 경우 isinstance() 확인하는 기능을 갖는다.
Btw 주어진 목록이 올바르지 않습니다! 당신은 너무 관용적 방법을 사용하여이 시나리오를 달성하기 위해 여러 가지 방법이 있습니다
>>> l = [1, 2, 3, 'a', 4, 11, 212, 'a', 'b', 'c', 3, 32, 4, 'd', 'e']
>>> [item for item in l if isinstance(item, str)]
['a', 'a', 'b', 'c', 'd', 'e']
당신은을 사용할 수 있습니다 지금까지 난 단지 제거하는 방법/추출 목록에서 특정 문자열이나 번호가 아닌 데이터 유형 :(
이입력을 찾을 수 type()
다른 데이터 유형을 구별하는 기능
https : //docs.python .org/3/library/functions.html # type – Craicerjack
@Craicerjack 감사합니다. –
a = [1,2,3,'a',4,11,212,'a','b','c',3,32,4,'d','e']
[x for x in a if type(x)==str]
출력된다 :
['a', 'a', 'b', 'c', 'd', 'e']
,
a = [1,2,3,'a',4,11,212,'a','b','c',3,32,4,'d','e']
print [item for item in a if type(item) is str]
Output list: ['a', 'a', 'b', 'c', 'd', 'e']
[파이썬의
가능한 중복 : 목록에서 int로 convertable되지 않는 항목을 제거하기 위해 깨끗하고 효율적인 방법 ] (http://stackoverflow.com/questions/29913324/python-clean-and-efficient-way-to-remove-items-that-are-not-convertable-to-int) –