如何在 Python 中的字典理解中创建值列表
How to create a list of values in a dictionary comprehension in Python
举一个非常简单的例子,循环一个句子并创建一个映射 {x:y}
的字典,其中 x
是表示单词长度的键,而 y
是句子中包含 x
字母数量
的单词列表
输入:
mywords = "May your coffee be strong and your Monday be short"
预期输出:
{2: ['be', 'be'], 3: ['May', 'and'], 4: ['your', 'your'], 5: ['short'], 6: ['coffee', 'strong', 'Monday']}
这是创建值列表但每次都覆盖它的尝试:
{len(x):[x] for x in mywords.split()}
{2: ['be'], 3: ['and'], 4: ['your'], 5: ['short'], 6: ['Monday']}
是否可以在 Python 中的一行中完成此操作?
当然可以,使用 sorted
+ groupby
,但看起来不太好。
from itertools import groupby
d = dict([(k, list(g)) for k, g in groupby(sorted(mywords.split(), key=len), key=len)])
print(d)
{2: ['be', 'be'],
3: ['May', 'and'],
4: ['your', 'your'],
5: ['short'],
6: ['coffee', 'strong', 'Monday']}
P.S., 这是我的 (using defaultdict
that I recommend over this) to the .
不要试图在一行中塞满所有内容,否则将无法阅读。这是一个简单易懂的解决方案,即使它需要几行代码:
from collections import defaultdict
mywords = "May your coffee be strong and your Monday be short"
ans = defaultdict(list)
for word in mywords.split():
ans[len(word)].append(word)
可以通过构建从 1 到单词的最大长度的原始字符串然后使用组并将它们的位置迭代为单词的大小来使用正则表达式。最后使用 defaultdict as set 将组中的单词添加到字典中。
text = "May your hot chocolate be delicious and sweet and your Monday be short"
max_len=0
for word in text.split():
if len(word)>max_len:
max_len=len(word)
pattern=[]
for index in range(1,max_len+1):
index=str(index)
pattern.append(r"(\b\w{"+"{index}".format(index=index)+r"}\b\s+)*")
pattern=''.join(pattern)
print(pattern)
groups=re.findall(pattern,text)
dict = defaultdict(set)
for group in groups:
for position,value in enumerate(group):
if len(value)>0:
dict[position+1].add(value)
print(dict)
输出:
defaultdict(<class 'set'>, {3: {'May ', 'hot ', 'and '}, 4: {'your '}, 9: {'delicious ', 'chocolate '}, 2: {'be '}, 5: {'sweet '}, 6: {'Monday '}})
举一个非常简单的例子,循环一个句子并创建一个映射 {x:y}
的字典,其中 x
是表示单词长度的键,而 y
是句子中包含 x
字母数量
输入:
mywords = "May your coffee be strong and your Monday be short"
预期输出:
{2: ['be', 'be'], 3: ['May', 'and'], 4: ['your', 'your'], 5: ['short'], 6: ['coffee', 'strong', 'Monday']}
这是创建值列表但每次都覆盖它的尝试:
{len(x):[x] for x in mywords.split()}
{2: ['be'], 3: ['and'], 4: ['your'], 5: ['short'], 6: ['Monday']}
是否可以在 Python 中的一行中完成此操作?
当然可以,使用 sorted
+ groupby
,但看起来不太好。
from itertools import groupby
d = dict([(k, list(g)) for k, g in groupby(sorted(mywords.split(), key=len), key=len)])
print(d)
{2: ['be', 'be'],
3: ['May', 'and'],
4: ['your', 'your'],
5: ['short'],
6: ['coffee', 'strong', 'Monday']}
P.S., 这是我的 defaultdict
that I recommend over this) to the
不要试图在一行中塞满所有内容,否则将无法阅读。这是一个简单易懂的解决方案,即使它需要几行代码:
from collections import defaultdict
mywords = "May your coffee be strong and your Monday be short"
ans = defaultdict(list)
for word in mywords.split():
ans[len(word)].append(word)
可以通过构建从 1 到单词的最大长度的原始字符串然后使用组并将它们的位置迭代为单词的大小来使用正则表达式。最后使用 defaultdict as set 将组中的单词添加到字典中。
text = "May your hot chocolate be delicious and sweet and your Monday be short"
max_len=0
for word in text.split():
if len(word)>max_len:
max_len=len(word)
pattern=[]
for index in range(1,max_len+1):
index=str(index)
pattern.append(r"(\b\w{"+"{index}".format(index=index)+r"}\b\s+)*")
pattern=''.join(pattern)
print(pattern)
groups=re.findall(pattern,text)
dict = defaultdict(set)
for group in groups:
for position,value in enumerate(group):
if len(value)>0:
dict[position+1].add(value)
print(dict)
输出:
defaultdict(<class 'set'>, {3: {'May ', 'hot ', 'and '}, 4: {'your '}, 9: {'delicious ', 'chocolate '}, 2: {'be '}, 5: {'sweet '}, 6: {'Monday '}})