所以我试图将大量列表中的一堆值附加到存储在另一个列表中的相应标题中。例如,现在我有一个只包含标题的列表(例如['目标'],['选择']),我想获取与这些标题相对应的值(例如& #39;目标':3,'选择':1)并将它们附加到标题列表中的相应值,同时从第二个列表中的值中删除标题 - 所以':'需要在原始列表中附加相应的值。然后我想在新列表中获取每个字符串(例如[Target:1,2,1,3 ...])并将它们导入到csv或excel文件的列中。我承认自己有点像菜鸟,但到目前为止我工作非常努力。以下是我没有说的内容(不包括导出到csv,因为我不知道该怎么做)。
key_name = ['distractor_shape','stimulus_param','prompt_format','test_format','d0_type','d1_type','d2_type','encoding_rt','verification_rt', 'target', 'choice','correct','debrief','response','CompletionCode']
def headers(list):
brah_list = []
for dup in list:
z = dup.count(',')
nw = dup.split(',',z)
brah_list.append(nw)
return brah_list
def parse_data(filename):
big_list = []
with open(filename) as f:
for word in f:
x = word.count(',')
new_word = word.split(',',x)
big_list.append(new_word)
return big_list
b_list = parse_data('A1T6RFUU0OTS0M.txt')
k_list = headers(key_name)
def f_un(things):
for t in things:
return t
h_list = f_un(k_list)
def f_in(stuff):
for sf in stuff:
for s in sf:
print(s)
z = 0
head_r = "h_list[z]"
if s.startswith(head_r):
s.strip(head_r)
h_list.append(s)
z += 1
print(stuff)
f_in(b_list)
答案 0 :(得分:0)
如果我正确理解你的问题,你有一个列表,并且你在另一个列表中有一些相应的值报复第一个列表。如果是这种情况,并假设您的所有值都按照正确的顺序对应,则可以使用列表理解:
lst1 = ['th', 'an', 'bu']
lst2 = ['e', 'd', 't']
#here
lst1 = [lst1[i] + lst2[i] for i in range(len(lst1))]
#here
print(lst1) #output: ['the', 'and', 'but']
答案 1 :(得分:0)
我仍然很难理解这个问题,但如果我的猜测是正确的,我认为这就是你所需要的。请注意,此代码使用了优秀的tablib
module:
from collections import defaultdict
import re
import tablib
data = defaultdict(list)
# input.txt:
# [t_show:[1471531808217,1471531809222,1471531809723,1471531812159,1471531815049],t_remove:[1471531809222,1471531809722,1471531812158,1471531815046,null],param:target:0,distractor_shape:square,stimulus_param:shape1:star,shape2:plus,relation:below,negate:false,prompt_format:verbal,test_format:visual,d0_type:shape:false,order:false,relation:true,d1_type:shape:2,order:true,relation:false,d2_type:shape:2,order:true,relation:true,encoding_rt:2435,verification_rt:2887,target:0,choice:0,correct:true]
# I had to remove "debrief", "response", and "CompletionCode", since those weren't in the example data
headers = ['distractor_shape','stimulus_param','prompt_format','test_format','d0_type','d1_type','d2_type','encoding_rt','verification_rt', 'target', 'choice','correct']#,'debrief','response','CompletionCode']
with open('input.txt', 'rt') as f:
# The input data seems to have some zero-width unicode characters interspersed.
# This next line removes them.
input = f.read()[1:-1].replace('\u200c\u200b', '')
# This matches things like foo:bar,... as well as foo:[1,2,3],...
for item in re.findall(r'[^,\[]+(?:\[[^\]]*\])?[^,\[]*', input):
# Split on the first colon
key, value = item.partition(':')[::2]
data[key].append(value)
dataset = tablib.Dataset()
dataset.headers = headers
for row in zip(*(data[header] for header in headers)):
dataset.append(row)
with open('out.csv', 'wt') as f:
f.write(dataset.csv)
# out.csv:
# distractor_shape,stimulus_param,prompt_format,test_format,d0_type,d1_type,d2_type,encoding_rt,verification_rt,target,choice,correct
# square,shape1:star,verbal,visual,shape:false,shape:2,shape:2,2435,2887,0,0,true
with open('out.xlsx', 'wb') as f:
f.write(dataset.xlsx)