programing tip

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

itbloger 2020. 6. 25. 21:26
반응형

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


Python 2.6에서 집합을 목록으로 변환하려고합니다. 이 구문을 사용하고 있습니다 :

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

그러나 다음과 같은 스택 추적이 나타납니다.

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

이 문제를 어떻게 해결할 수 있습니까?


이미 목록입니다

type(my_set)
>>> <type 'list'>

당신은 같은 것을 원하십니까

my_set = set([1,2,3,4])
my_list = list(my_set)
print my_list
>> [1, 2, 3, 4]

편집 : 마지막 코멘트 출력

>>> my_list = [1,2,3,4]
>>> my_set = set(my_list)
>>> my_new_list = list(my_set)
>>> print my_new_list
[1, 2, 3, 4]

나는 당신이 이런 식으로했는지 궁금합니다.

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

대신에:

first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

왜 프로세스를 단축시키지 않겠습니까?

my_list = list(set([1,2,3,4])

그러면 목록에서 듀피가 제거되고 목록이 다시 표시됩니다.


[편집] 이전에 "list"를 다음과 같이 변수 이름으로 사용하여 재정의 한 것 같습니다.

list = set([1,2,3,4]) # oops
#...
first_list = [1,2,3,4]
my_set=set(first_list)
my_list = list(my_set)

그리고 당신은 얻을

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

이러한 유형의 문제에 빠질 때마다 다음을 사용하여 먼저 변환하려는 요소의 데이터 유형을 찾으십시오.

type(my_set)

그런 다음 다음을 사용하십시오.

  list(my_set) 

목록으로 변환합니다. 파이썬에서 새로 만든 목록을 일반 목록처럼 사용할 수 있습니다.


Review your first line. Your stack trace is clearly not from the code you've pasted here, so I don't know precisely what you've done.

>>> my_set=([1,2,3,4])
>>> my_set
[1, 2, 3, 4]
>>> type(my_set)
<type 'list'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

What you wanted was set([1, 2, 3, 4]).

>>> my_set = set([1, 2, 3, 4])
>>> my_set
set([1, 2, 3, 4])
>>> type(my_set)
<type 'set'>
>>> list(my_set)
[1, 2, 3, 4]
>>> type(_)
<type 'list'>

The "not callable" exception means you were doing something like set()() - attempting to call a set instance.


Simply type:

list(my_set)

This will turn a set in the form {'1','2'} into a list in the form ['1','2'].


I'm not sure that you're creating a set with this ([1, 2]) syntax, rather a list. To create a set, you should use set([1, 2]).

These brackets are just envelopping your expression, as if you would have written:

if (condition1
    and condition2 == 3):
    print something

There're not really ignored, but do nothing to your expression.

Note: (something, something_else) will create a tuple (but still no list).


Python is a dynamically typed language, which means that you cannot define the type of the variable as you do in C or C++:

type variable = value

or

type variable(value)

In Python, you use coercing if you change types, or the init functions (constructors) of the types to declare a variable of a type:

my_set = set([1,2,3])
type my_set

will give you <type 'set'> for an answer.

If you have a list, do this:

my_list = [1,2,3]
my_set = set(my_list)

Hmmm I bet that in some previous lines you have something like:

list = set(something)

Am I wrong ?

참고URL : https://stackoverflow.com/questions/6593979/how-to-convert-a-set-to-a-list-in-python

반응형