찾다
백엔드 개발파이썬 튜토리얼Python에서 단어 수 계산 아이디어에 대한 자세한 설명

이 기사에서는 Python에서 단어 수 계산에 대한 자세한 아이디어를 주로 소개합니다. 또한 타사 모듈을 사용하지 않는 솔루션도 제공합니다. 관심 있는 친구들은 함께 살펴보세요

문제 설명:

Python 사용 문자열 s와 숫자 n을 가져와 s에서 가장 자주 나타나는 n개의 단어를 반환하는 count_words() 함수를 구현하세요. 반환 값은 발생 빈도가 가장 높은 n 단어와 해당 시간을 포함하는 튜플 목록입니다. 즉, [(, ), (, )입니다. ;), ... ], 발생 순서대로 정렬됩니다.

모든 입력은 소문자이고 구두점이나 기타 문자가 포함되어 있지 않다고 가정할 수 있습니다(문자와 단일 공백만). 발생 횟수가 동일한 경우 알파벳 순서로 정렬됩니다.

예:

print count_words("betty bought a bit of butter but the butter was bitter",3)

출력:

[('butter', 2), ('a', 1), ('betty', 1)]

해결 아이디어 문제 :

1. 문자열 s를 공백으로 분할하여 ['betty', 'bought', 'a', 'bit', 'of', 'butter', ' but', 'the', 'butter', 'was', 'bitter']

2. 맵리스트를 생성하고 Split_s를 다음과 같은 튜플 목록으로 변환합니다: [('betty', 1), ( ' 샀다', 1), ('a', 1), ('bit', 1), ('of', 1), ('버터', 1), ('but', 1), ('the ' , 1), ('butter', 1), ('was', 1), ('bitter', 1)]

3. 튜플의 첫 번째 인덱스 값이 동일한 경우 맵리스트의 요소를 병합합니다. , 두 번째 인덱스 값이 추가됩니다.

// 참고: defaultdict 사용을 준비하세요. 획득한 데이터는 다음과 같습니다: {'betty': 1, 'bought': 1, 'a': 1, 'bit': 1, 'of': 1, 'butter': 2, 'but': 1, 'the': 1, 'was': 1, 'bitter': 1}

4. 키를 기준으로 알파벳순으로 정렬하면 다음과 같은 결과가 나옵니다: [('a', 1), ('betty', 1) , ( '비트', 1), ('쓴', 1), ('구입', 1), ('그러나', 1), ('버터', 2), ('의', 1), ( 'the ', 1), ('was', 1)]

5. 2차 정렬을 수행하고 값을 기준으로 정렬하여 다음을 얻습니다: [('butter', 2), ('a', 1), ( '베티', 1), ('비트', 1), ('쓴', 1), ('샀다', 1), ('그러나', 1), ('의', 1), ('the ', 1), ('was', 1)]

6. 슬라이싱을 사용하여 더 높은 빈도로 * 데이터 그룹을 추출합니다

요약: defaultdict가 없는 정렬 결과는 python3에서도 정확하지만 python2에서는 올바르지 않습니다. defaultdict 자체에는 순서가 없습니다. 목록을 구별하려면 정렬해야 합니다.

타사 모듈을 사용하지 않고 직접 작성해 볼 수도 있습니다.

해결책 1(defaultdict 사용):

from collections import defaultdict
"""Count words."""
def count_words(s, n):
  """Return the n most frequently occuring words in s."""
  split_s = s.split()
  map_list = [(k,1) for k in split_s]
  output = defaultdict(int)
  for d in map_list:
    output[d[0]] += d[1]
  output1 = dict(output)
  top_n = sorted(output1.items(), key=lambda pair:pair[0], reverse=False)
  top_n = sorted(top_n, key=lambda pair:pair[1], reverse=True)
  return top_n[:n]
def test_run():
  """Test count_words() with some inputs."""
  print(count_words("cat bat mat cat bat cat", 3))
  print(count_words("betty bought a bit of butter but the butter was bitter", 4))
if __name__ == '__main__':
  test_run()

해결책 2(카운터 사용)

from collections import Counter
"""Count words."""
def count_words(s, n):
  """Return the n most frequently occuring words in s."""
  split_s = s.split()
  split_s = Counter(name for name in split_s)
  print(split_s)
  top_n = sorted(split_s.items(), key=lambda pair:pair[0], reverse=False)
  print(top_n)
  top_n = sorted(top_n, key=lambda pair:pair[1], reverse=True)
  print(top_n)
  return top_n[:n]
def test_run():
  """Test count_words() with some inputs."""
  print(count_words("cat bat mat cat bat cat", 3))
  print(count_words("betty bought a bit of butter but the butter was bitter", 4))
if __name__ == '__main__':
  test_run()

관련 권장 사항:

파이 값을 임의의 숫자로 계산하는 방법에 대한 Python 예

위 내용은 Python에서 단어 수 계산 아이디어에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
파이썬 어레이에 어떤 데이터 유형을 저장할 수 있습니까?파이썬 어레이에 어떤 데이터 유형을 저장할 수 있습니까?Apr 27, 2025 am 12:11 AM

PythonlistsCanstoreAnyDatAtype, ArrayModuLearRaysStoreOneType 및 NUMPYARRAYSAREFORNUMERICALPUTATION.1) LISTSAREVERSATILEBUTLESSMEMORY-EFFICENT.2) ARRAYMODUERRAYRAYRAYSARRYSARESARESARESARESARESARESAREDOREDORY-UNFICEDONOUNEOUSDATA.3) NumpyArraysUraysOrcepperperperperperperperperperperperperperperperferperferperferferpercient

파이썬 어레이에 잘못된 데이터 유형의 값을 저장하려고하면 어떻게됩니까?파이썬 어레이에 잘못된 데이터 유형의 값을 저장하려고하면 어떻게됩니까?Apr 27, 2025 am 12:10 AM

whenyouattempttoreavalueofthewrongdatatypeinapythonaphonarray, thisiSdueTotheArrayModule의 stricttyPeenforcement, theAllElementStobeofthesAmetypecified bythetypecode.forperformancersassion, arraysaremoreficats the thraysaremoreficats thetheperfication the thraysaremorefications는

Python Standard Library의 일부는 무엇입니까? 목록 또는 배열은 무엇입니까?Python Standard Library의 일부는 무엇입니까? 목록 또는 배열은 무엇입니까?Apr 27, 2025 am 12:03 AM

Pythonlistsarepartoftsandardlardlibrary, whileraysarenot.listsarebuilt-in, 다재다능하고, 수집 할 수있는 반면, arraysarreprovidedByTearRaymoduledlesscommonlyusedDuetolimitedFunctionality.

스크립트가 잘못된 파이썬 버전으로 실행되는지 확인해야합니까?스크립트가 잘못된 파이썬 버전으로 실행되는지 확인해야합니까?Apr 27, 2025 am 12:01 AM

thescriptIsrunningwithHongpyThonversionDueCorRectDefaultTerpretersEttings.tofixThis : 1) checktheDefaultPyThonVersionUsingPyThon-VersionorPyThon3- version.2) usvirtual-ErondmentsBythePython.9-Mvenvmyenv, 활성화, 및 파괴

파이썬 어레이에서 수행 할 수있는 일반적인 작업은 무엇입니까?파이썬 어레이에서 수행 할 수있는 일반적인 작업은 무엇입니까?Apr 26, 2025 am 12:22 AM

PythonArraysSupportVariousOperations : 1) SlicingExtractsSubsets, 2) 추가/확장 어드먼트, 3) 삽입 값 삽입 ATSpecificPositions, 4) retingdeletesElements, 5) 분류/ReversingChangesOrder 및 6) ListsompectionScreateNewListSbasedOnsistin

어떤 유형의 응용 프로그램에서 Numpy Array가 일반적으로 사용됩니까?어떤 유형의 응용 프로그램에서 Numpy Array가 일반적으로 사용됩니까?Apr 26, 2025 am 12:13 AM

NumpyArraysareSentialplosplicationSefficationSefficientNumericalcomputationsanddatamanipulation. Theyarcrucialindatascience, MachineLearning, Physics, Engineering 및 Financeduetotheiribility에 대한 handlarge-scaledataefficivally. forexample, Infinancialanyaly

파이썬의 목록 위의 배열을 언제 사용 하시겠습니까?파이썬의 목록 위의 배열을 언제 사용 하시겠습니까?Apr 26, 2025 am 12:12 AM

UseanArray.ArrayOveralistInpyThonWhendealingwithhomogeneousData, Performance-CriticalCode, OrinterFacingwithCcode.1) HomogeneousData : ArraysSaveMemorywithtypepletement.2) Performance-CriticalCode : arraysofferbetterporcomanceFornumericalOperations.3) Interf

모든 목록 작업은 배열에 의해 지원됩니까? 왜 또는 왜 그렇지 않습니까?모든 목록 작업은 배열에 의해 지원됩니까? 왜 또는 왜 그렇지 않습니까?Apr 26, 2025 am 12:05 AM

아니요, NOTALLLISTOPERATIONARESUPPORTEDBYARRARES, andVICEVERSA.1) ArraySDONOTSUPPORTDYNAMICOPERATIONSLIKEPENDORINSERTWITHUTRESIGING, WHITHIMPACTSPERFORMANCE.2) ListSDONOTEECONSTANTTIMECOMPLEXITEFORDITITICCESSLIKEARRAYSDO.

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구