gpt4 book ai didi

python - 嵌套循环列表并动态创建变量

转载 作者:太空狗 更新时间:2023-10-30 02:24:30 24 4
gpt4 key购买 nike

我有一个句子列表,我想每次对两个句子执行一些操作,但不是对所有句子。

例如:

list= ["aaaaa","bbbbb","ccccc","ddddd","eeeee"]
similarity_a-d = sim("aaaaa","ddddd")
similarity_a-e = sim("aaaaa","eeeee")
similarity_b-d = sim("bbbbb","ddddd")
similarity_b-e = sim("bbbbb","eeeee")
similarity_c-d = sim("ccccc","ddddd")
similarity_c-e = sim("ccccc","eeeee")

这就是我的尝试:

similarity={}
for i,vec_lda_topic in enumerate(vec_lda_topics)[:numOfUSs]:
for j,vec_lda_topic in enumerate(vec_lda_topics)[numOfUSs:]:
similarity["sim{0}-{1}".format(i,j)] = gensim.matutils.cossim(vec_lda_topics[i], vec_lda_topics[j])
print('similarity between docs ', i, ' and ',j,': ', similarity["sim{0}-{1}".format(i,j)])

并收到以下错误:

TypeError: 'enumerate' object is not subscriptable

除了错误之外,也许还有更好的方法来做到这一点?

最佳答案

枚举切片列表,不切片枚举列表(两处都这样做)

for i,vec_lda_topic in enumerate(vec_lda_topics[:numOfUSs]):

关于python - 嵌套循环列表并动态创建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53159443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com