사용자가 이름 목록을 입력하고 컴퓨터가 중복 된 이름을 인쇄하는 프로그램을 만들고 있습니다. 지금까지 가지고있는 코드는 다음과 같습니다.
names = []
final = []
enter = raw_input('Enter the name')
while enter != 'exit':
names.append(enter)
enter = raw_input('Enter the name')
for i in names:
for a in (names):
a = i + 1
if a == i:
final.append(i)
print final
도달하면 오류가 발생합니다.
a = i + 1
이 문제를 어떻게 해결할 수 있습니까?
이 작업을 수행하여 중복을 찾을 수 있습니다 ( for
루프 대신 ).
print set(x for x in names if names.count(x) > 1)
이것은 변수에 두 번 이상 set
나타나는 값 을 반환합니다 names
.
names = []
final = []
enter = raw_input('Enter the name')
while enter != 'exit':
names.append(enter)
enter = raw_input('Enter the name')
print(set(x for x in names if names.count(x) > 1))
산출:
Enter the nameAndy
Enter the nameAndy
Enter the nameAndy
Enter the nameBob
Enter the nameGeorge
Enter the nameAndy
Enter the nameBob
Enter the nameexit
set(['Bob', 'Andy'])
Bob
그리고 Andy
한 번 이상 입력되었다. George는 아니 었으므로 세트에 나타나지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다