【问题标题】:I want to get string from list我想从列表中获取字符串
【发布时间】:2020-02-19 07:16:55
【问题描述】:

下面有一个矩阵

[[a1, b, c],
[d2, e, a1],
[c1, dfd, u],
....

]

如果与特定单词匹配,则从矩阵返回索引。 并找到行固定列。 然后我将矩阵转换为列表。 我得到这样的结果["RESULT DATA"]。 我只想在没有[''] 的情况下获取字符串RESULT DATA

r, c = np.where(Matrix == SpecificWord)
find_row = np.where(c == 0) 
result_data = Matrix[find_row,1].tolist()
print(str(result_data ))
print(result_data[0])

【问题讨论】:

  • print(data[0]) 的输出是什么?
  • 你的意思是np.argwhere(Matrix==SpecificWord)?
  • print(data[0]) 的输出是 ['RESULT DATA']
  • 我只使用 np.where 而不是 argwhere,我要求我只想从列表中提取字符串。因为当我将 result_data 写入文件时,result_data 会打印为 ['result data'] 而不是 result data。
  • Matrixnp.matrix 对象吗?不是普通的ndarrayMatrix[k,1].shape 是什么?什么是“k”?

标签: python python-3.x list numpy


【解决方案1】:

解决了

row, col = np.where(Terms == ElemData)
cList = col .tolist()
rLits = row.tolist()
if 0 not in cList:
    return None
else:
    num_col = cList.index(0)
    num_row = rLits[num_col]
    data = Terms[num_row, 1].tolist()
    return data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2012-01-12
    • 2012-11-13
    • 1970-01-01
    • 2015-03-24
    • 2019-05-25
    • 1970-01-01
    相关资源
    最近更新 更多