내 질문은 다음과 유사 이 있지만 약간의 차이. 파일을 읽고 '보낸 사람'으로 시작하는 이메일이 포함 된 줄을 찾은 다음이 이메일을 저장할 사전을 만들고 최대 발생 이메일 주소도 제공하려고합니다.
파일에서 찾을 줄은 다음과 같습니다.
발신 : [email protected] 2008 년 1 월 5 일 토요일 09:14:16
이것이 발견 될 때마다 이메일 부분을 추출한 다음 사전을 만들기 전에 목록에 배치해야합니다.
dict에서 최대 키, 값을 인쇄하기 위해이 코드 샘플을 찾았습니다.
counts = dict()
names = ['csev','owen','csev','zqian','cwen']
for name in names:
counts[name] = counts.get(name,0) + 1
maximum = max(counts, key = counts.get)
print maximum, counts[maximum]
이 샘플 코드에서 다음 프로그램으로 시도했습니다.
import re
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
matches = []
addy = []
counts = dict()
for lines in handle :
# look for specific characters in document text
if not lines.startswith("From ") : continue
# increment the count variable for each math found
lines.split()
# append the required lines to the matches list
matches.append(lines)
# loop through the list to acess each line individually
for email in matches :
# place values in variable
out = email
# looking through each line for any email add found
found = re.findall(r'[\w\.-]+@[\w\.-]+', out)
# loop through the found emails and print them out
for i in found :
i.split()
addy.append(i)
for i in addy:
counts[i] = counts.get(i, 0) + 1
maximum = max(counts, key=counts.get)
print counts
print maximum, counts[maximum]
이제 문제는 from으로 시작하는 27 줄만 있고 해당 목록에서 가장 많이 반복되는 이메일은 5 번 발생하는 '[email protected]'여야하지만 코드를 실행하면 출력이 다음과 같아야한다는 것입니다.
{'[email protected]': 1640, '[email protected]': 7207, 'cwen@
iupui.edu': 8888, '[email protected]': 1911, '[email protected]': 10678, '
[email protected]': 10140, '[email protected]': 4205, '[email protected]'
: 2500, '[email protected]': 16804, '[email protected]': 7490, 'ray@media
.berkeley.edu': 168}
이해를 돕기위한 텍스트 파일 링크 : 텍스트 파일
몇 가지 문제가 있습니다.
첫 번째 for email in matches
는 텍스트 파일의 각 행에 대해 루프가 호출된다는 것입니다.
for lines in handle :
# look for specific characters in document text
if not lines.startswith("From ") : continue
# increment the count variable for each math found
lines.split()
# append the required lines to the matches list
matches.append(lines)
# loop through the list to acess each line individually
for email in matches:
그래서 그 변화로 당신은 알다시피 경기를 한 번 반복합니다.
그런 다음 각 경기에서 하나만 있다는 것을 알고 있으므로 찾기를 다음과 같이 변경할 수 있습니다.
found = re.findall(r'[\w\.-]+@[\w\.-]+', out)[0]
내가 변경 한 각 항목의 수를 계산하려면 :
# loop through the found emails and print them out
for i in found :
i.split()
addy.append(i)
for i in addy:
counts[i] = counts.get(i, 0) + 1
maximum = max(counts, key=counts.get)
더 읽기 쉽게 :
if found in counts:
counts[found] += 1
else:
counts[found] = 1
그런 다음 항상 다음과 같이 저장하는 대신 끝에 최대 값을 얻을 수 있습니다.
print counts
print max(counts, key=lambda x : x[1])
통합하면 다음과 같은 이점이 있습니다.
import re
name = raw_input("Enter file:")
if len(name) < 1 :
name = "mbox-short.txt"
handle = open(name)
matches = []
addy = []
counts = dict()
for lines in handle :
# look for specific characters in document text
if not lines.startswith("From ") : continue
# increment the count variable for each math found
lines.split()
# append the required lines to the matches list
matches.append(lines)
# loop through the list to acess each line individually
for email in matches:
# place values in variable
out = email
# looking through each line for any email add found
found = re.findall(r'[\w\.-]+@[\w\.-]+', out)[0]
# loop through the found emails and print them out
if found in counts:
counts[found] += 1
else:
counts[found] = 1
print counts
print max(counts, key=lambda x : x[1])
다음을 반환합니다.
{'[email protected]': 1, '[email protected]': 3, '[email protected]': 5, '[email protected]': 1, '[email protected]': 2, '[email protected]': 3, '[email protected]': 4, '[email protected]': 1, '[email protected]': 4, '[email protected]': 2, '[email protected]': 1}
[email protected]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다