2009-08-18 45 views
-1

這裏的最高金額的名字是我與如何找到一個ASCII值之和在文件中查找ASCII的最大數量,並打印出ASCII值

def ascii_sum(): 
    x = 0 
    infile = open("30075165.txt","r") 
    for line in infile: 
     return sum([ord(x) for x in line]) 
    infile.close() 
工作的代碼

此代碼只打印出文件沒有最大ASCII值在第一個ASCII值

+1

您無法展開您以前的問題? – SilentGhost 2009-08-18 12:04:20

+2

我已經有了解決這個問題的更新你的上一個問題:http://stackoverflow.com/questions/1292630/how-to-open-a-file-and-find-the-longest-length-of- a-line-and-then-print-it-out/1292676#1292676我敢打賭,這也回答你的下一個問題。 – kjfletch 2009-08-18 12:09:26

回答

1
max(open(fname), key=lambda line: sum(ord(i) for i in line)) 
+0

+1,如果他想要他可以做的行號:max(enumerate(open(fname)),key = lambda(i,line):sum(ord(i)for i in line)) – 2009-08-18 12:11:13

1

這是一個答案,一個片段到你的previous questions

def get_file_data(filename): 
    def ascii_sum(line): 
     return sum([ord(x) for x in line]) 
    def word_count(line): 
     return len(line.split(None)) 

    filedata = [{'line': line, 
       'line_len': len(line), 
       'ascii_sum': ascii_sum(line), 
       'word_count': word_count(line)} 
       for line in open(filename, 'r')] 

    return filedata 

afile = r"C:\Tmp\TestFile.txt" 
file_data = get_file_data(afile) 

print max(file_data, key=lambda line: line['line_len']) # Longest Line 
print max(file_data, key=lambda line: line['ascii_sum']) # Largest ASCII sum 
print max(file_data, key=lambda line: line['word_count']) # Most Words