-1

说我有以下的话我想放在一个列表中

"cat,dog,fish"         (first row)
"turtle,charzard,pikachu,lame"    (second row)
"232.34,23.4,242.12%"           (third row)

我的问题是我如何计算每一行中的标记,比如第一行有 3 个,第二行有 4 个,第三行有 3 个。之后我如何计算字符,然后为每一行确定哪个令牌有最多字符?所以输出看起来像

token count = 3, character count = 10, fish has the most characters
token count = 4, character count = 25, charzard has the most characters
token count = 3, character count = 17, 242.12% has the most characters

只使用像 len() 这样的简单列表方法。并使用逗号作为分隔符。谢谢,我真的迷路了,因为每次我尝试使用 strip(',') 删除逗号时都会出错

4

4 回答 4

4

尝试这个。适用于两者Python2Python3

rows = [ "cat,dog,fish", "turtle,charzard,pikachu,lame", "232.34,23.4,242.12%" ]
for row in rows:
    tokens = row.split(',')
    token_cnt = len(tokens)
    char_cnt = sum([len(token) for token in tokens])
    longest_token = max(tokens, key=len)
    print("token count = %d, character count = %d, %s has the most characters" %(token_cnt, char_cnt, longest_token))

结果:

>>> token count = 3, character count = 10, fish has the most characters
>>> token count = 4, character count = 25, charzard has the most characters
>>> token count = 3, character count = 17, 242.12% has the most characters

编辑:

现在,受@inspectorG4dget 的回答启发,现在使用max而不是我愚蠢的选择来查找最长的单词。sort

于 2013-10-07T06:37:10.043 回答
1

给定一个字符串列表:

def my_output(string_of_tokens):
    tokens = string_of_tokens.split(",")
    print "token count = %s, character count = %s, %s has the most characters" %
        (len(tokens), sum(map(len, tokens)), reduce(lambda a, b: a if len(a) > len(b) else b, tokens))

list = ["cat,dog,fish", "turtle,charzard,pikachu,lame", "232.34,23.4,242.12%"]
for l in list:
    my_output(l)
于 2013-10-07T06:43:23.907 回答
1

假设您有一个逗号分隔行的文件:

with open('path/to/input') as infile:
  for i,line in enumerate(infile, 1):
    toks = line.split(',')
    print "row %d: token_count=%d character_count=%d '%s' has the most characters" %(len(toks), sum(len(t) for t in toks), max(toks, key=len))
于 2013-10-07T06:49:33.723 回答
-2

To count the number of tokens per line

Try this

import re
print len(re.findall(r'\w+', line))

DEMO

于 2013-10-07T06:33:50.680 回答