我有一个用逗号分隔的字符串,
。我想从给定的字符串中找出最长的字符串
words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
到目前为止我做了什么
print(max(words.split(','), key=len))
我得到的输出是abcdefghi
,但是你可以看到abcdefghi
和barracuda
的长度相同。那么,为什么我只得到一个而不是两个或全部呢?
还
words = 'fishes,sam,gollum,sauron,frodo,balrog'
在上面的string
中,有许多字的长度相同。我要返回每一个
您可以将word
的zip
len改为word
,然后从len
创建dict
,并返回最大的len
,如下所示:
>>> from collections import defaultdict
>>> words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
>>> dct = defaultdict(list)
>>> lstWrdSplt = words.split(',')
>>> for word, length in (zip(lstWrdSplt,(map(len,lstWrdSplt)))):
... dct[length].append(word)
>>> dct[max(dct)]
['abcdefghi', 'barracuda']
# for more explanation
>>> dct
defaultdict(list,
{3: ['run'],
4: ['barn', 'fish', 'swim'],
9: ['abcdefghi', 'barracuda'],
6: ['yellow'],
5: ['shark']})
你可以用它作为function
,用regex
来查找words
,如下所示:
from collections import defaultdict
import re
def mxLenWord(words):
dct = defaultdict(list)
lstWrdSplt = re.findall('w+', words)
for word, length in (zip(lstWrdSplt,(map(len,lstWrdSplt)))):
dct[length].append(word.strip())
return dct[max(dct)]
words = 'rUnNiNg ,swimming, eating,biking, climbing'
mxLenWord(words)
输出:
['swimming', 'climbing']
试试下面的
from collections import defaultdict
data = defaultdict(list)
words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
for w in words.split(','):
data[len(w)].append(w)
word_len = sorted(data.keys(),reverse=True)
for wlen in word_len:
print(f'{wlen} -> {data[wlen]}')
输出9 -> ['abcdefghi', 'barracuda']
6 -> ['yellow']
5 -> ['shark']
4 -> ['barn', 'fish', 'swim']
3 -> ['run']
对于这样一个简单的任务,我发现有很多方法太复杂了。您可以使用sorted()
和groupby()
的组合来解决它:
from itertools import groupby
words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
_, (*longest,) = next(groupby(sorted(words.split(","), key=len, reverse=True), len))
print(longest)
要查找所有相同长度的单词,可以使用下一行:
from itertools import groupby
words = 'fishes,sam,gollum,sauron,frodo,balrog'
words_len = {l: list(w) for l, w in groupby(sorted(words.split(","), key=len), len)}
print(words_len)