如何根据新字典中列表值强加的标准对字典值进行排序?
How to sort values of a dictionary based on the criteria imposed by values of a list in a new dictionary?
对标题表示歉意,想不出一种表达方式,如果有人有建议,我很乐意编辑!
基本上,我有字典和列表。词典:
dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
榜单:
list1 = [('alphabet'), ('number'), ('neither')]
我想做的是根据列表中的项目对字典键的值进行排序,return一个新字典,其中的值现在是 dict1 的键和新的键是列表中的项目。
('alphabet')的值是字母时应该是dict1的键,('number')的值是数字时应该是dict1的键,并且('neither') 的值应该是当键的值在两种情况下都失败时。
例如我的输出应该是:
{('alphabet'): [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], ('number'): [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], ('neither'): [('period', 'exclamation mark')]}
我开始创建一个新字典,其中包含列表中的键和空列表的值,我想在满足上述要求时添加值(可能通过使用 .isdigit() 和 .isalpha ()) 到空列表中。但是我真的不明白如何在通过要求时将值添加到特定键?
我是初学者,非常简单,没有导入模块或列表理解的简短代码将是理想的,但任何指导将不胜感激!谢谢!
您可以遍历字典中的键、值对,将键附加到适当的列表,并使用它们形成新字典。
dict1 = {('red fruit', 'green fruit'): 'apple',
('orange fruit', 'pink fruit'): 'peach',
('five fruit', 'one fruit'): 51,
('ten fruit', 'nine fruit'): 19,
('period', 'exclamation mark'): '.!'}
list1 = [('alphabet'), ('number'), ('neither')]
alphabetical = []
numerical = []
other = []
for key, value in dict1.items():
if isinstance(value, int):
numerical.append(key)
elif value.isalpha():
alphabetical.append(key)
else:
other.append(key)
list2 = {'alphabet': alphabetical,
'number': numerical,
'neither': other}
print(list2)
请注意您使用的地方,例如('foo')
这与 'foo'
相同(尽管上面我保留了您在分配 list1
时使用的语法)。
这里我用了isinstance(..., int)
因为你的数据项是整数。 isdigit
只有当它们是包含数字的字符串时才相关。
另请注意,如果 所有 字符串中的字符都是按字母顺序排列的,则 isalpha
只会评估 True
。该方法主要用于单个字符,但问题没有指定如果字符串包含多种字符类型的混合会发生什么,所以我将其保留在这里。但是,例如,如果您在其中一个字典值的中间有一个 space,那么 isalpha
将 return False
即使其余字符是按字母顺序排列的。
如果我的理解正确,您想根据值的类型过滤掉字典键,然后按照单独列表中所述的类型对键进行排序。
要确定字符串的类型,您需要更准确地了解什么算作 'alphabet',而不是仅仅列出您要定义的事物的名称。例如:
def string_type(s):
if s.isalpha():
return 'alphabet'
elif s.isdigit():
return 'number'
else:
return 'neither'
然后,您可以继续筛选出您想要的键,并按照您在list1
中指定的顺序将它们放入字典中
def process(dict1, list1):
output = {k: [] for k in list1}
for key, val in dict1.items():
t = string_type( str(val) )
if t in list1:
output[t].append(key)
return output
示例输出:
>>> dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
>>> list1 = [('alphabet'), ('number'), ('neither')]
>>> process(dict1, list1)
{'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], 'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')]}
>>> list2 = [('number'), ('neither'), ('alphabet')]
>>> process(dict1, list2)
{'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')], 'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')]}
对标题表示歉意,想不出一种表达方式,如果有人有建议,我很乐意编辑!
基本上,我有字典和列表。词典:
dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
榜单:
list1 = [('alphabet'), ('number'), ('neither')]
我想做的是根据列表中的项目对字典键的值进行排序,return一个新字典,其中的值现在是 dict1 的键和新的键是列表中的项目。
('alphabet')的值是字母时应该是dict1的键,('number')的值是数字时应该是dict1的键,并且('neither') 的值应该是当键的值在两种情况下都失败时。
例如我的输出应该是:
{('alphabet'): [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], ('number'): [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], ('neither'): [('period', 'exclamation mark')]}
我开始创建一个新字典,其中包含列表中的键和空列表的值,我想在满足上述要求时添加值(可能通过使用 .isdigit() 和 .isalpha ()) 到空列表中。但是我真的不明白如何在通过要求时将值添加到特定键?
我是初学者,非常简单,没有导入模块或列表理解的简短代码将是理想的,但任何指导将不胜感激!谢谢!
您可以遍历字典中的键、值对,将键附加到适当的列表,并使用它们形成新字典。
dict1 = {('red fruit', 'green fruit'): 'apple',
('orange fruit', 'pink fruit'): 'peach',
('five fruit', 'one fruit'): 51,
('ten fruit', 'nine fruit'): 19,
('period', 'exclamation mark'): '.!'}
list1 = [('alphabet'), ('number'), ('neither')]
alphabetical = []
numerical = []
other = []
for key, value in dict1.items():
if isinstance(value, int):
numerical.append(key)
elif value.isalpha():
alphabetical.append(key)
else:
other.append(key)
list2 = {'alphabet': alphabetical,
'number': numerical,
'neither': other}
print(list2)
请注意您使用的地方,例如('foo')
这与 'foo'
相同(尽管上面我保留了您在分配 list1
时使用的语法)。
这里我用了isinstance(..., int)
因为你的数据项是整数。 isdigit
只有当它们是包含数字的字符串时才相关。
另请注意,如果 所有 字符串中的字符都是按字母顺序排列的,则 isalpha
只会评估 True
。该方法主要用于单个字符,但问题没有指定如果字符串包含多种字符类型的混合会发生什么,所以我将其保留在这里。但是,例如,如果您在其中一个字典值的中间有一个 space,那么 isalpha
将 return False
即使其余字符是按字母顺序排列的。
如果我的理解正确,您想根据值的类型过滤掉字典键,然后按照单独列表中所述的类型对键进行排序。
要确定字符串的类型,您需要更准确地了解什么算作 'alphabet',而不是仅仅列出您要定义的事物的名称。例如:
def string_type(s):
if s.isalpha():
return 'alphabet'
elif s.isdigit():
return 'number'
else:
return 'neither'
然后,您可以继续筛选出您想要的键,并按照您在list1
def process(dict1, list1):
output = {k: [] for k in list1}
for key, val in dict1.items():
t = string_type( str(val) )
if t in list1:
output[t].append(key)
return output
示例输出:
>>> dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
>>> list1 = [('alphabet'), ('number'), ('neither')]
>>> process(dict1, list1)
{'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], 'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')]}
>>> list2 = [('number'), ('neither'), ('alphabet')]
>>> process(dict1, list2)
{'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')], 'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')]}