同时枚举两个列表
Enumerate two lists at the same time
我有两个列表:
1.List 个 IPA 符号 - M
2.List 个单词 - N
现在我需要创建第三个列表 X = [N,M],其中对于在单个单词中找到的每个 IPA 符号,我必须为新列表分配 1 和 0。例如,如果 M = ['ɓ ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b'] 并且为简单起见,N 只有两个词 = ['ɓuli', 'rutə'],那么输出应该如下所示
X = [[1,1,1,1,0,0,0,0,0,0],
[0,1,0,0,1,1,1,0,0,0]]
所以它是一种共现矩阵,但更简单 - 因为我不需要计算符号在单词中出现的次数。当符号出现在适当位置的单词中时,我只需要将 1 分配给 X。也许我想太多了,但我似乎无法找到一种方法来保存两个列表的索引。
这是我的代码片段:
M = ['ɓ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b']
N = ['ɓuli', 'rutə']
X = np.zeros((len(N), len(M)))
for n_idx in range(len(N)):
print('Current word index', n_idx)
for symbol in N[n_idx]:
if symbol in M:
print(symbol, 'found, at word index', n_idx, ', and symbol index')
# if found then ad to X at proper position
#Expected result
X = [[1,1,1,1,0,0,0,0,0,0],
[0,1,0,0,1,1,1,0,0,0]]
你可以这样做。只需循环您需要与其他列表检查的单词并进行比较。
M=['a','e','i','o','u']
N=['stack','overflow']
output=[]
for words in N:
words_output=[]
for v in M:
o = 1 if v in words else 0
words_output.append(o)
output.append(words_output)
output:
[[1, 0, 0, 0, 0], [0, 1, 0, 1, 0]]
试试这个
M = ['ɓ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b']
N = ['ɓuli', 'rutə']
result = []
for n in N:
tmp = []
characters = list(n)
for m in M:
tmp.append(1 if m in characters else 0)
result.append(tmp)
print(result)
#[[1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
# [0, 1, 0, 0, 1, 1, 1, 0, 0, 0]]
您可以使用此行构建这样的索引:
X = [[1 if e in s else 0 for e in M] for s in N]
这是一个在字母和单词上循环的双重理解列表。但是,您应该使用 sklearn 等库来更有效地执行此类操作(例如 https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html)
我有两个列表: 1.List 个 IPA 符号 - M 2.List 个单词 - N
现在我需要创建第三个列表 X = [N,M],其中对于在单个单词中找到的每个 IPA 符号,我必须为新列表分配 1 和 0。例如,如果 M = ['ɓ ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b'] 并且为简单起见,N 只有两个词 = ['ɓuli', 'rutə'],那么输出应该如下所示 X = [[1,1,1,1,0,0,0,0,0,0], [0,1,0,0,1,1,1,0,0,0]]
所以它是一种共现矩阵,但更简单 - 因为我不需要计算符号在单词中出现的次数。当符号出现在适当位置的单词中时,我只需要将 1 分配给 X。也许我想太多了,但我似乎无法找到一种方法来保存两个列表的索引。 这是我的代码片段:
M = ['ɓ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b']
N = ['ɓuli', 'rutə']
X = np.zeros((len(N), len(M)))
for n_idx in range(len(N)):
print('Current word index', n_idx)
for symbol in N[n_idx]:
if symbol in M:
print(symbol, 'found, at word index', n_idx, ', and symbol index')
# if found then ad to X at proper position
#Expected result
X = [[1,1,1,1,0,0,0,0,0,0],
[0,1,0,0,1,1,1,0,0,0]]
你可以这样做。只需循环您需要与其他列表检查的单词并进行比较。
M=['a','e','i','o','u']
N=['stack','overflow']
output=[]
for words in N:
words_output=[]
for v in M:
o = 1 if v in words else 0
words_output.append(o)
output.append(words_output)
output:
[[1, 0, 0, 0, 0], [0, 1, 0, 1, 0]]
试试这个
M = ['ɓ', 'u', 'l', 'i', 'r', 't', 'ə', 'w', 'a', 'b']
N = ['ɓuli', 'rutə']
result = []
for n in N:
tmp = []
characters = list(n)
for m in M:
tmp.append(1 if m in characters else 0)
result.append(tmp)
print(result)
#[[1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
# [0, 1, 0, 0, 1, 1, 1, 0, 0, 0]]
您可以使用此行构建这样的索引:
X = [[1 if e in s else 0 for e in M] for s in N]
这是一个在字母和单词上循环的双重理解列表。但是,您应该使用 sklearn 等库来更有效地执行此类操作(例如 https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html)