如何展平多级元组并使用制表符分隔的元素?
How to flatten multiple levels of tuple and have tab-separated elements?
这是我在导入 xlwings 时编写的计算单词相似度的函数,xlwings 是一个 python-excel 库。我希望它 return 像这样(我期望的是,每个 row/line 中的项目应该被一个选项卡分开。然后我可以很容易地 copy/paste 变成 Excel求和的文件),例如:
0.9999998807907104 'casual' 1.0 1.0 29.0
0.8386740684509277 'active' 0.3333 1.0 13.0
0.776314377784729 'cardigans'0.1667 1.0 84.0
但它实际上 return 是这样的(我讨厌的是,我无法复制到 Excel 文件以供进一步使用,例如求和数字):
[[0.9999998807907104, ('casual', (1.0, 1.0, 29.0))],
[0.8386740684509277, ('active', (0.3333, 1.0, 13.0))],
[0.776314377784729, ('cardigans', (0.1667, 1.0, 84.0))]]
我怎么会意识到这一点?谢谢。
def similarity(phrase, N=10):
phrase_vec = phrase_model[phrase]
CosDisList = []
wb = xw.Book('file01.xlsx')
sht = wb.sheets['sheet1']
for a_word in phrase_model.keys():
a_val = phrase_model[a_word]
cos_dis = cosine_similarity(phrase_vec, a_val)
for i in range(1, 18):
if a_word == sht.cells(i, 1).value:
DataFromExcel = (sht.cells(i, 2).value, sht.cells(i, 3).value, sht.cells(i, 4).value)
DataCombined = (a_word, DataFromExcel)
CosDisBind = [float(str(cos_dis.tolist()).strip('[[]]')), DataCombined]
CosDisList.append(CosDisBind)
CosDisListSort = sorted(CosDisList, key=operator.itemgetter(0), reverse=True)
CosDisListTopN = heapq.nlargest(N, CosDisListSort)
return CosDisListTopN
您可以使用以下功能。资料来源:a blogpost
def flatten(l, ltypes=(list, tuple)):
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)
然后只需使用:
abc = [[0.9999998807907104, ('casual', (1.0, 1.0, 29.0))],
[0.8386740684509277, ('active', (0.3333, 1.0, 13.0))],
[0.776314377784729, ('cardigans', (0.1667, 1.0, 84.0))]]
flat_list = flatten(abc)
final_array = np.array(flat_list).reshape((np.round(len(flat_list)//5), 5)).tolist()
# [['0.9999998807907104', 'casual', '1.0', '1.0', '29.0'], ['0.8386740684509277', 'active', '0.3333', '1.0', '13.0'], ['0.776314377784729', 'cardigans', '0.1667', '1.0', '84.0']]
现在您可以加入个人列表:
most_final = ["\t".join(x) for x in final_array]
print(most_final[0])
输出
print(most_final[0])
0.9999998807907104 casual 1.0 1.0 29.0
这是我在导入 xlwings 时编写的计算单词相似度的函数,xlwings 是一个 python-excel 库。我希望它 return 像这样(我期望的是,每个 row/line 中的项目应该被一个选项卡分开。然后我可以很容易地 copy/paste 变成 Excel求和的文件),例如:
0.9999998807907104 'casual' 1.0 1.0 29.0
0.8386740684509277 'active' 0.3333 1.0 13.0
0.776314377784729 'cardigans'0.1667 1.0 84.0
但它实际上 return 是这样的(我讨厌的是,我无法复制到 Excel 文件以供进一步使用,例如求和数字):
[[0.9999998807907104, ('casual', (1.0, 1.0, 29.0))],
[0.8386740684509277, ('active', (0.3333, 1.0, 13.0))],
[0.776314377784729, ('cardigans', (0.1667, 1.0, 84.0))]]
我怎么会意识到这一点?谢谢。
def similarity(phrase, N=10):
phrase_vec = phrase_model[phrase]
CosDisList = []
wb = xw.Book('file01.xlsx')
sht = wb.sheets['sheet1']
for a_word in phrase_model.keys():
a_val = phrase_model[a_word]
cos_dis = cosine_similarity(phrase_vec, a_val)
for i in range(1, 18):
if a_word == sht.cells(i, 1).value:
DataFromExcel = (sht.cells(i, 2).value, sht.cells(i, 3).value, sht.cells(i, 4).value)
DataCombined = (a_word, DataFromExcel)
CosDisBind = [float(str(cos_dis.tolist()).strip('[[]]')), DataCombined]
CosDisList.append(CosDisBind)
CosDisListSort = sorted(CosDisList, key=operator.itemgetter(0), reverse=True)
CosDisListTopN = heapq.nlargest(N, CosDisListSort)
return CosDisListTopN
您可以使用以下功能。资料来源:a blogpost
def flatten(l, ltypes=(list, tuple)):
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)
然后只需使用:
abc = [[0.9999998807907104, ('casual', (1.0, 1.0, 29.0))],
[0.8386740684509277, ('active', (0.3333, 1.0, 13.0))],
[0.776314377784729, ('cardigans', (0.1667, 1.0, 84.0))]]
flat_list = flatten(abc)
final_array = np.array(flat_list).reshape((np.round(len(flat_list)//5), 5)).tolist()
# [['0.9999998807907104', 'casual', '1.0', '1.0', '29.0'], ['0.8386740684509277', 'active', '0.3333', '1.0', '13.0'], ['0.776314377784729', 'cardigans', '0.1667', '1.0', '84.0']]
现在您可以加入个人列表:
most_final = ["\t".join(x) for x in final_array]
print(most_final[0])
输出
print(most_final[0])
0.9999998807907104 casual 1.0 1.0 29.0