字典值列表
List of Dictionary Values
我有如下字典:
tf ={1: ['a', 'b', 'c'], 2: ['d', 'x', 'y']}
我想将值列表形成为:
wrd_list = ['a', 'b', 'c', 'd', 'x', 'y']
我尝试使用以下代码来做到这一点:
wrd_list = []
for k, v in tf.items():
for i in v:
word_list.append(i)
有什么有效的方法吗?
当你想连接列表时,在这里使用 extend 比 append 更快:
word_list = []
for v in tf.values():
word_list.extend(v)
我有如下字典:
tf ={1: ['a', 'b', 'c'], 2: ['d', 'x', 'y']}
我想将值列表形成为:
wrd_list = ['a', 'b', 'c', 'd', 'x', 'y']
我尝试使用以下代码来做到这一点:
wrd_list = []
for k, v in tf.items():
for i in v:
word_list.append(i)
有什么有效的方法吗?
当你想连接列表时,在这里使用 extend 比 append 更快:
word_list = []
for v in tf.values():
word_list.extend(v)