下面有一张标签的清单。
mytags = ["a", "b", "c", "d", "e", "f"]还有,我有一个文件,文件中有这样的字符串,以列表格式显示。
['a-1', 'b-3', 'c-4', 'e-3']
['a-10', 'b-12', 'c-14', 'd-16']
['b-1', 'c-5', 'd-13', 'f-7']我想把文件打印成一个标签分隔的表,按照我的列表中标签的顺序排列,如下所示。
#header
#a, b, c, d, e, f
a-1 b-3 c-4 NA e-3 NA
a-10 b-12 c-14 d-16 NA NA
NA b-1 c-5 d-13 NA f-7我编写了python代码,但是嵌套的双循环提供了一个不想要的结果。
print (mylist)
for lineList in file:
for tag in mytags:
if tag in lineList:
print(lineList, end="\t")
else:
print("NA", end="\t")如何用这些数据制作表格?
发布于 2018-10-16 09:54:50
可以在这里使用setdefault
my_tags = ["a", "b", "c", "d", "e", "f"]
line_list = [
['a-1', 'b-3', 'c-4', 'e-3'],
['a-10', 'b-12', 'c-14', 'd-16'],
['b-1', 'c-5', 'd-13', 'f-7']
]
for lst in line_list:
d = {i[0]: i for i in lst}
for i in my_tags:
print(d.setdefault(i, 'NA'), end ='\t')
print()
a-1 b-3 c-4 NA e-3 NA
a-10 b-12 c-14 d-16 NA NA
NA b-1 c-5 d-13 NA f-7 发布于 2018-10-16 08:40:30
在与标签列表进行比较之前,您应该从项目中提取这些标记:
mytags = ["a", "b", "c", "d", "e", "f"]
rows = [
['a-1', 'b-3', 'c-4', 'e-3'],
['a-10', 'b-12', 'c-14', 'd-16'],
['b-1', 'c-5', 'd-13', 'f-7']
]
for row in rows:
for tag in mytags:
print(row.pop(0) if row and row[0].split('-')[0] == tag else 'NA', end='\t')
print()或使用生成器表达式:
print('\n'.join('\t'.join(row.pop(0) if row and row[0].split('-')[0] == tag else 'NA' for tag in mytags) for row in rows))发布于 2018-10-16 08:58:21
因为字符串将在一个文件中,所以下面是我的方法
# read the file
data = pd.read_csv('test.txt', header=None,sep='[')
master_df = pd.DataFrame(columns=['a','b','c','d','e','f'])
for i in range(len(data)):
master_df.loc[i] = 'NA'
temp = data[1][i].replace(']','')
temp = temp.replace("'",'')
for char in temp.split(','):
master_df[char.split('-')[0].strip()][i] = char
print(master_df)输出
a b c d e f
0 a-1 b-3 c-4 NA e-3 NA
1 a-10 b-12 c-14 d-16 NA NA
2 NA b-1 c-5 d-13 NA f-7https://stackoverflow.com/questions/52831043
复制相似问题