programing

목록으로 설정된 Python

bestprogram 2023. 5. 22. 21:52

목록으로 설정된 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'>

지도와 람다 함수의 조합을 사용해 보십시오.

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

문자열에 숫자 집합이 있고 정수 목록으로 변환하려는 경우 매우 편리한 방법입니다.

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

언급URL : https://stackoverflow.com/questions/6828722/python-set-to-list