在Python中转换数组的List对象

时间:2017-02-13 21:41:18

标签: python arrays list

我有这个列表对象

[('migrations',), ('users',), ('devices',), ('externals',), ('cloud_securities',), ('operators',), ('promotions',), ('visitors',), ('caches',), ('captures',), ('mirror_settings',), ('wifis',), ('service_plans',), ('auto_provisionings',), ('guest_settings',), ('help_texts',), ('gateways',), ('fb_wifi_tokens',), ('health_check',), ('ubb_settings',), ('templates',), ('ubb_user_settings',), ('captive_portals',), ('languages',)]

我试图将它们作为数组列表

我试过

# table_names = np.asarray(table_names)

我得到了

[['migrations']
 ['users']
 ['devices']
 ['externals']
 ['cloud_securities']
 ['operators']
 ['promotions']
 ['visitors']
 ['caches']
 ['captures']
 ['mirror_settings']
 ['wifis']
 ['service_plans']
 ['auto_provisionings']
 ['guest_settings']
 ['help_texts']
 ['gateways']
 ['fb_wifi_tokens']
 ['health_check']
 ['ubb_settings']
 ['templates']
 ['ubb_user_settings']
 ['captive_portals']
 ['languages']]

但我希望它像这样

['migrations', 'users', 'devices', 'externals', 'cloud_securities', 'operators', 'promotions', 'visitors', 'caches', 'captures', 'mirror_settings', 'wifis', 'service_plans', 'auto_provisionings', 'guest_settings', 'help_texts', 'gateways', 'fb_wifi_tokens', 'health_check', 'ubb_settings', 'templates', 'ubb_user_settings', 'captive_portals', 'languages']

我该怎么做那个python?

我应该使用或研究哪些功能?

1 个答案:

答案 0 :(得分:5)

你不希望tuple想要简单的python&列表理解:

说出newl = [x[0] for x in l] 是你的清单,先把每个放在第一位。仅itertools.chain元素的项目并从那里重建列表:

newl = list(itertools.chain.from_iterable(l))

一般的解决方案,如果你的元组包含多于1个元素会导致#include <string> #include <sstream> int add(int a, int b) { return a + b; } int main() { std::string str = "1 2"; std::stringstream ss(str); int a, b; ss >> a; ss >> b; printf("Result: %d\n", add(a, b)); } 使列表变平,但这可能是一种过度杀伤:

int a, b;
ss >> a;
ss >> b;
printf("Result: %d\n", add(a, b));