dict的Python列表
我有这本字典的目录dict的Python列表,python,list,dictionary,Python,List,Dictionary,我有这本字典的目录 dictList = [ {'value': 'me'}, {'value': 'you'}, {'value': 'him'}, {'value': 'her'}, {'value': 'them'}, {'value': 'they'} ] 我知道如何得到这样的键的值 print(item for item in dictlist if item["value"] == "me").next() 打印出来的 {'value': 'me'} 但是,我只
dictList = [
{'value': 'me'}, {'value': 'you'}, {'value': 'him'},
{'value': 'her'}, {'value': 'them'}, {'value': 'they'}
]
我知道如何得到这样的键的值
print(item for item in dictlist if item["value"] == "me").next()
打印出来的
{'value': 'me'}
但是,我只想打印“我”、“你”等,而不是“值”。所以结果看起来像
{'me', 'you', 'him', 'her','them', 'they'}
谢谢您可以使用列表理解提取要查找的值,如果不需要保留,也可以将其用作生成器表达式
[d['value'] for d in dictList if 'value' in d]
只有当dict中存在键'value'
时,才会执行d['value']
查找
如果你知道所有的dict都有这个键,你可以删除过滤器
[d['value'] for d in dictList]
您可以使用列表理解提取要查找的值,如果不需要保留它,也可以将其用作生成器表达式
[d['value'] for d in dictList if 'value' in d]
只有当dict中存在键'value'
时,才会执行d['value']
查找
如果你知道所有的dict都有这个键,你可以删除过滤器
[d['value'] for d in dictList]
另一种方法是从列表中的所有词典中收集
值()。这会奏效的
无论使用什么键(使用“值”)
另一种方法是从列表中的所有词典中收集值()。这会奏效的
无论使用什么键(使用“值”)
使用reduce
如果您喜欢非常不可读的方法,这里有一个:
>>> reduce(list.__add__, map(dict.values, dictList))
>>> ['me', 'you', 'him', 'her', 'them', 'they']
解释:
dict.values
:这是一种方法,通常应用于
方法:{“key”:12,“sleutel”:44}.values()
并仅返回dict中所有键的值,
因此[12,44]
通过dict.values
我们明确地引用了该方法,并调用map
将其应用于
您的口述列表
从map(dict.values,dictList)
你可以得到[['me']、['you']、['him']、['her']、['them']、['them']、['them']
然后将一个子列表添加到另一个子列表
要在列表中执行此操作,请使用reduce
,它以以下方式执行操作:
>>> res = ['me'] + ['you']
>>> res = res + ['him']
>>> res = res + ['her']
>>> res = res + ['them']
>>> res = res + ['they']
最后你得到了你想要的
使用sum
通过提供[]
的初始值,可以缩短解决方案的时间:
>>> sum(map(dict.values, dictList), [])
>>> ['me', 'you', 'him', 'her', 'them', 'they']
使用reduce
如果您喜欢非常不可读的方法,这里有一个:
>>> reduce(list.__add__, map(dict.values, dictList))
>>> ['me', 'you', 'him', 'her', 'them', 'they']
解释:
dict.values
:这是一种方法,通常应用于
方法:{“key”:12,“sleutel”:44}.values()
并仅返回dict中所有键的值,
因此[12,44]
通过dict.values
我们明确地引用了该方法,并调用map
将其应用于
您的口述列表
从map(dict.values,dictList)
你可以得到[['me']、['you']、['him']、['her']、['them']、['them']、['them']
然后将一个子列表添加到另一个子列表
要在列表中执行此操作,请使用reduce
,它以以下方式执行操作:
>>> res = ['me'] + ['you']
>>> res = res + ['him']
>>> res = res + ['her']
>>> res = res + ['them']
>>> res = res + ['they']
最后你得到了你想要的
使用sum
通过提供[]
的初始值,可以缩短解决方案的时间:
>>> sum(map(dict.values, dictList), [])
>>> ['me', 'you', 'him', 'her', 'them', 'they']
另一个dicts问题列表!尝试使用pandas包简单地将键引用为列标题,并使用相同的键对值进行分组:
import pandas as pd
dictList = [
{'value': 'me'}, {'value': 'you'}, {'value': 'him'},
{'value': 'her'}, {'value': 'them'}, {'value': 'they'}
]
df = pd.DataFrame(dictList)
dictList_values = df['value'].tolist()
添加了一些基准测试,使用pandas在大型集合上表现出色。在这里,我创建了一个包含200k条目的集合,作为“大型”案例。小案例是给定的6个条目:
setup_large = "dictList = [];\
[dictList.extend(({'value': 'me'}, {'value': 'you'}, {'value': 'him'},\
{'value': 'her'}, {'value': 'them'}, {'value': 'they'})) for _ in range(25000)];\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(dictList);"
setup_small = "dictList = [];\
dictList.extend(({'value': 'me'}, {'value': 'you'}, {'value': 'him'},\
{'value': 'her'}, {'value': 'them'}, {'value': 'they'}));\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(dictList);"
method1 = "[d['value'] for d in dictList if 'value' in d]"
method2 = "df['value'].tolist()"
import timeit
t = timeit.Timer(method1, setup_small)
print('Small Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_small)
print('Small Method Pandas: ' + str(t.timeit(100)))
t = timeit.Timer(method1, setup_large)
print('Large Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_large)
print('Large Method Pandas: ' + str(t.timeit(100)))
#Small Method LC: 0.000217914581299
#Small Method Pandas: 0.00168395042419
#Large Method LC: 3.89074897766
#Large Method Pandas: 0.189871072769
另一个dicts问题列表!尝试使用pandas包简单地将键引用为列标题,并使用相同的键对值进行分组:
import pandas as pd
dictList = [
{'value': 'me'}, {'value': 'you'}, {'value': 'him'},
{'value': 'her'}, {'value': 'them'}, {'value': 'they'}
]
df = pd.DataFrame(dictList)
dictList_values = df['value'].tolist()
添加了一些基准测试,使用pandas在大型集合上表现出色。在这里,我创建了一个包含200k条目的集合,作为“大型”案例。小案例是给定的6个条目:
setup_large = "dictList = [];\
[dictList.extend(({'value': 'me'}, {'value': 'you'}, {'value': 'him'},\
{'value': 'her'}, {'value': 'them'}, {'value': 'they'})) for _ in range(25000)];\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(dictList);"
setup_small = "dictList = [];\
dictList.extend(({'value': 'me'}, {'value': 'you'}, {'value': 'him'},\
{'value': 'her'}, {'value': 'them'}, {'value': 'they'}));\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(dictList);"
method1 = "[d['value'] for d in dictList if 'value' in d]"
method2 = "df['value'].tolist()"
import timeit
t = timeit.Timer(method1, setup_small)
print('Small Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_small)
print('Small Method Pandas: ' + str(t.timeit(100)))
t = timeit.Timer(method1, setup_large)
print('Large Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_large)
print('Large Method Pandas: ' + str(t.timeit(100)))
#Small Method LC: 0.000217914581299
#Small Method Pandas: 0.00168395042419
#Large Method LC: 3.89074897766
#Large Method Pandas: 0.189871072769
你确定这就是你想要的数据结构吗?你想要的结果是一个集合
,因此原始顺序将丢失。你确定这就是你想要的数据结构吗?你想要的结果是一个集合
,因此原始顺序将丢失