2014-01-10 162 views
-2

如果我有一個如下所示的列表元素。如何通過\ t字符分割來創建包含多個元素的列表? ['county_id\temp_2010\temp_2020\temp_2030\temp_2040\thhld_2010\thhld_2020\thhld_2030\thhld_2040\tmf_2010\tmf_2020\tmf_2030\tmf_2040\tpop_2010\tpop_2020\tpop_2030\tpop_2040\tdu_2010\tdu_2020\tdu_2030\tdu_2040\tsf_2010\tsf_2020\tsf_2030\tsf_2040']通過分割' t'來分割python中的列表元素

+0

嘗試:'my_list [0] .split( '\ t')' –

回答

3
>>> mylist = ['county_id\temp_2010\temp_2020\temp_2030\temp_2040\thhld_2010\thhld_2020\thhld_2030\thhld_2040\tmf_2010\tmf_2020\tmf_2030\tmf_2040\tpop_2010\tpop_2020\tpop_2030\tpop_2040\tdu_2010\tdu_2020\tdu_2030\tdu_2040\tsf_2010\tsf_2020\tsf_2030\tsf_2040'] 
>>> splitlist = mylist[0].split("\t") 
>>> splitlist 
['county_id', 'emp_2010', 'emp_2020', 'emp_2030', 'emp_2040', 'hhld_2010', 'hhld_2020', 'hhld_2030', 'hhld_2040', 'mf_2010', 'mf_2020', 'mf_2030', 'mf_2040', 'pop_2010', 'pop_2020', 'pop_2030', 'pop_2040', 'du_2010', 'du_2020', 'du_2030', 'du_2040', 'sf_2010', 'sf_2020', 'sf_2030', 'sf_2040'] 

應該做的伎倆......

+0

燁這樣做了。我正在嘗試類似的東西,但我忘了指定我的列表的[0]索引。謝謝 –

+0

+1 :)擊敗我秒 –

+2

@PauloBu 22秒。永恆的:) – MattDMo