简体   繁体   English

python中的循环索引问题

[英]Trouble with loop indexing in python

I'm having trouble indexing a result in a list.我无法在列表中索引结果。

The function "Similar" calculates the similarity between two strings in a range from 0 to 1. (Example: similar('dino','bino') = 0.75)函数“Similar”计算两个字符串在 0 到 1 范围内的相似度。(示例:similar('dino','bino') = 0.75)

What I want to do is to iterate all items in each sublist of x with all items in list y.我想要做的是用列表 y 中的所有项目迭代 x 的每个子列表中的所有项目。 And find the domain with the highest similarity for each sublist of x.并为 x 的每个子列表找到相似度最高的域。

My expected output would be:我的预期输出是:

['smart phones', NaN, 'fruits']

Here's my code so far:到目前为止,这是我的代码:

x = [['phones', 'galaxy samsung', 'iphone'],[],['fruit', 'food']] ##each sublist refers to one user
y = ['fruits', 'smart phones', 'fishing', 'cars']                 ##domains

point = [0] * len(x)
best_dom = ['n'] * len(x)

for list in x:
i=0
  for query in list:
    for dom in y:
        sim = similar(query,dom)
        if sim > point[i]:
            point[i] = sim
            best_dom[i] = dom
i = i+1

print(best_dom)

But this is the output I'm getting:但这是我得到的输出:

['fruits', 'n', 'n']

My solution script consist of two parts:我的解决方案脚本由两部分组成:

  1. Generating a list similar to x with its sublists.生成一个类似于x的列表及其子列表。
    These hold tuples according to (<item>, <similarity_value>) storing the item and its highest similarity value to any item in y .这些根据(<item>, <similarity_value>)保存元组(<item>, <similarity_value>)存储项目及其与y任何项目的最高相似度值。
  2. Getting the max-value of each sublist or 'NaN' if the sublist is empty.如果子列表为空,则获取每个子列表的最大值'NaN'

-> afterwards printing the output list -> 之后打印输出列表

# this will  be similar to x but storing the similarity_value with alongside each item
similarity_values = []

# iterating over x to generate values of similarity_values
for i, sublist in enumerate(x):
    similarity_values.append([])  # creating new sublist inside
    for item in sublist:  # iterating over each item in a sublist
        similarity_values[i].append((item, similar(item)))

# outputting the max values
output_list = []
for sublist in similarity_values:
    if sublist == []:  # 'NaN' if list is empty
        output_list.append('NaN')
    else:  # otherwise item with highest similarity_value
        output_list.append(max(sublist, key=lambda e: e[1])[0])
        # key for max() is set to the second element of the tuple

print(output_list)

I hope this will solve your problem!我希望这能解决你的问题!

I got it!我知道了! I was not addressing the index in the correct way.我没有以正确的方式处理索引。

x = [['phones', 'galaxy samsung', 'iphone'],[],['fruit', 'food']] ##each sublist refers to one user
y = ['fruits', 'smart phones', 'fishing', 'cars']                 ##domains

point = [0] * len(x)
best_dom = ['n'] * len(x)

for user in x:
  for query in user:
    for dom in y:
      i = x.index(user)
      sim = similar(query,dom)
      if sim > point[i]:
            point[i] = sim
            best_dom[i] = dom
                
print(best_dom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM