如何使用Python从列表数组创建简单列表?

时间:2016-09-08 06:49:18

标签: python python-2.7

以下是清单:

  • [[('mobile','VB')],[('margin','NN')],[('and','CC')]]

但我想从列表中删除[]。输出应该是:

  • [('mobile','VB'),('margin','NN'),('and','CC')]

4 个答案:

答案 0 :(得分:1)

使用列表理解

L = [[('mobile','VB')],[('margin','NN')],[('and','CC')]]
R = [ x[0] for x in L ]

答案 1 :(得分:0)

d={'a':'apple','b':'ball'}
d.keys()   # will give you all keys in list
['a','b']
d.values()  # will give you values in list
['apple','ball']
d.items()  # will give you pair tuple of key and value
[('a','apple'),('b','ball')
print keys,values method one

for x in d.keys():
    print x +" => " + d[x]  

'

var arr= []; // create an empty array

    arr.push({
        key:   "Name",
        value: "value"
    });
 OR

    var array = {};

    array ['key1'] = "testing1";
    array ['key2'] = "testing2";
    array ['key3'] = "testing3";
    console.log(array);

or like that
    var input = [{key:"key1", value:"value1"},{key:"key2", value:"value2"}];

答案 2 :(得分:0)

一种解决方案是迭代列表并将第一项放入其中并将其添加到新列表中。

In [1] _list = [[('mobile','VB')],[('margin','NN')],[('and','CC')]]
In [2] cleaned = []
In [3] for item in _list: cleaned.append(item[0])

In [4] print(cleaned)
Out[4] [('mobile', 'VB'), ('margin', 'NN'), ('and', 'CC')]

答案 3 :(得分:0)

您可以使用itertools.chain

import itertools
def unlist_items(list1):
    return list(itertools.chain(*list1))

list1 = [[('mobile','VB')],[('margin','NN')],[('and','CC')] ]
print( unlist_items( list1 ) ) # prints [('mobile', 'VB'), ('margin', 'NN'), ('and', 'CC')]