Tho*_*nes 2 python list add cpu-word
问题:如何从列表中删除第一个单词以添加到名为的新列表中car_list,并将其余单词添加到另一个列表中other_list.
在other_list我把其余的到字典中
比如我读文件时得到的东西
data_file = ['1911 Overland OctoAuto', '1913 Scripps-Booth Bi-Autogo','1920 Briggs and Stratton Flyer'
car_list = []
other_list = []
我如何得到如下结果
car_list = [Overland, Scripps-Booth, Briggs]
other_list = [1911,OctoAuto, 1913, Bi-Autogo, 1920, and Stratton flyer]
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的
data_file = open("facts.txt", 'r')
def clean_list(data_file):
new_list =[]
clean_list =[]
car_list = []
other_list = []
D = {}
for i in data_file:
new_list = data_file.split('\n') #change split by new line or word
clean_list = [(x.strip(' ')) for x in new_list]
car_list = (clean_list.strip().split(' ')[2:], ' ')
other_list = dict(zip(keys, values))# Just an example
return car_list
car_list = clean_list(data_file)
Run Code Online (Sandbox Code Playgroud)
我想 car_list = (clean_list.strip().split(' ')[2:], ' ')
会工作,但我得到以下错误.
car_list = (clean_list.lstrip().split(' ')[2:], ' ')
Run Code Online (Sandbox Code Playgroud)
AttributeError: 'list' object has no attribute 'split'
AttributeError: 'list' object has no attribute 'lstrip'
我认为通过拼接可以工作,但没有骰子
我试过car_list = clean_list.split(' ',2)[2]没打印任何东西
有任何想法吗?我知道该文件正在阅读中,但我不知道该怎么做.
我的警告是,它other_list看起来像是不同类型数据的混合物.这通常不明智.有了这个免责声明,这是一个尝试:
data_file = ['1911 Overland OctoAuto',
'1913 Scripps-Booth Bi-Autogo',
'1920 Briggs and Stratton Flyer']
car_list = []
other_list = []
for entry in data_file:
year, make, model = entry.split(' ',2)
car_list.append(make)
other_list.append(year)
other_list.append(model)
print car_list
>>>> ['Overland', 'Scripps-Booth', 'Briggs']
print other_list
>>>> ['1911', 'OctoAuto', '1913', 'Bi-Autogo', '1920', 'and Stratton Flyer']
Run Code Online (Sandbox Code Playgroud)