[python] 파이썬은 목록으로 설정

파이썬에서 세트를 목록으로 변환하는 방법은 무엇입니까? 사용

a = set(["Blah", "Hello"])
a = list(a)

작동하지 않습니다. 그것은 나에게 준다 :

TypeError: 'set' object is not callable



답변

코드 작동합니다 (cpython 2.4, 2.5, 2.6, 2.7, 3.1 및 3.2로 테스트).

>>> a = set(["Blah", "Hello"])
>>> a = list(a) # You probably wrote a = list(a()) here or list = set() above
>>> a
['Blah', 'Hello']

list실수로 덮어 쓰지 않았는지 확인하십시오 .

>>> assert list == __builtins__.list


답변

실수로 변수 세트로 사용하여 내장 세트를 음영 처리했습니다. 오류를 복제하는 간단한 방법은 다음과 같습니다.

>>> set=set()
>>> set=set()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

첫 번째 라인 은 세트를 세트 의 인스턴스 로 리 바인드 합니다. 두 번째 줄은 물론 인스턴스 를 호출 하려고 합니다.

다음은 각 변수에 다른 이름을 사용하는 혼동이 적은 버전입니다. 신선한 통역사 사용하기

>>> a=set()
>>> b=a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object is not callable

잘하면 전화 a는 오류입니다


답변

쓰기 전에 set(XXXXX)
“set”을 변수로 사용했습니다. 예 :

set = 90 #you have used "set" as an object


a = set(["Blah", "Hello"])
a = list(a)


답변

이것은 작동합니다 :

>>> t = [1,1,2,2,3,3,4,5]
>>> print list(set(t))
[1,2,3,4,5]

그러나 “list”또는 “set”을 변수 이름으로 사용하면 다음과 같은 결과가 나타납니다.

TypeError: 'set' object is not callable

예 :

>>> set = [1,1,2,2,3,3,4,5]
>>> print list(set(set))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

“list”를 변수 이름으로 사용하면 동일한 오류가 발생합니다.


답변

s = set([1,2,3])
print [ x for x in iter(s) ]


답변

코드는 Win7 x64에서 Python 3.2.1과 작동합니다.

a = set(["Blah", "Hello"])
a = list(a)
type(a)
<class 'list'>


답변

map과 lambda 함수의 조합을 사용해보십시오 :

aList = map( lambda x: x, set ([1, 2, 6, 9, 0]) )

문자열에 숫자 세트가 있고 정수 목록으로 변환하려는 경우 매우 편리한 접근 방식입니다.

aList = map( lambda x: int(x), set (['1', '2', '3', '7', '12']) )