简体   繁体   English

Python 从字典更新列表中的值?

[英]Python Update values in list from dict?

I'm trying to convert values in nested list from dict我正在尝试从 dict 转换嵌套列表中的值

list1 =  [["a","b"],["c"],["a","d"]]

list2 =  [["a","b"],["c"],[""],["a","d"]]

dict_form = {"a":"app","b":"bold","c":"cafe","d":"dot"}

Expected output预期 output

new_list = [["app","bold"],["cafe"],["app","dot"]]

result_1 = [["app","bold"],["cafe"],[""],["app","dot"]]

What I have tried:我试过的:

result = [[dict_form[i] for i in j] for j in new_list]

Use a 2-level list comprehension使用 2 级列表理解

result = [[dict_form[k] for k in sublist] for sublist in list1]
print(result) # [['app', 'bold'], ['cafe'], ['app', 'dot']]

Timeit on 20 loops of 200k iterations Timeit在 20 次 20 万次迭代的循环上

# List comprehension : [[dict_form[k] for k in sublist] for sublist in list1]
python -m timeit -r 20 -n 200000 -s "list1 =  [['a','b'],['c'],['a','d']];dict_form = {'a':'app','b':'bold','c':'cafe','d':'dot'}" "[[dict_form[k] for k in sublist] for sublist in list1]"
200000 loops, best of 20: 1.74 usec per loop

# list/map : list(map(lambda x: list(map(dict_form.get, x)), list1))
python -m timeit -r 20 -n 200000 -s "list1 =  [['a','b'],['c'],['a','d']];dict_form = {'a':'app','b':'bold','c':'cafe','d':'dot'}" "list(map(lambda x: list(map(dict_form.get, x)), list1))"
200000 loops, best of 20: 3.2 usec per loop

So more performant on normal list length, and same on huge list (see Derek post)所以在正常列表长度上性能更高,在大列表上也一样(见 Derek 帖子)

Here is how you can use a nested list comprehension:以下是如何使用嵌套列表推导:

list1 =  [["a","b"],["c"],["a","d"]]
dict_form = {"a":"app","b":"bold","c":"cafe","d":"dot"}
new_list = [[dict_form[k] for k in ls] for ls in list1]
print(new_list)

Output: Output:

[['app', 'bold'], ['cafe'], ['app', 'dot']]

another way would be to just use map:另一种方法是只使用 map:

list(map(lambda x: list(map(dict_form.get, x)), list1))

output: output:

[['app', 'bold'], ['cafe'], ['app', 'dot']]

slight variation:轻微变化:

[*map(lambda x: [*map(dict_form.get, x)], list1)]

As a follow up to @azro's timings, I generated a much larger input list1 with the same kind of structure..作为@azro 时间安排的后续行动,我生成了一个更大的输入 list1 具有相同的结构..

using the current input from the OP, approx:使用来自 OP 的当前输入,大约:

nested list comprehension = 0.000000833 s嵌套列表理解 = 0.000000833 s

map method = 0.000001440 s map 方法 = 0.000001440 s

map method variation = 0.000001180 s map 方法变化 = 0.000001180 s

with a MUCH larger but similar list, approx:有一个更大但相似的列表,大约:

nested list comprehension = 0.054890156 s嵌套列表理解 = 0.054890156 s

map method = 0.050899744 s map 方法 = 0.050899744 s

map method variation = 0.065859318 s map 方法变化 = 0.065859318 s

So I guess the most efficient solution (from those provided) depends on the size of your actual list, seems the map method tends to perform the best as the lists get larger所以我想最有效的解决方案(从提供的解决方案中)取决于您的实际列表的大小,似乎 map 方法往往会随着列表变大而表现最好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM