通过出现每个元素的情况和匹配的嵌套列表(字符串)的更改顺序,分别没有第一个元素
我想根据列表中的每个元素的匹配,也要根据列表中的匹配来更改列表中的顺序,也取决于有多少匹配。因此,如果在两个列表中出现一个字符串,则应相互订购。匹配越多,他们相互追随的优先级就越高。
但是,在该顺序中不应考虑每个列表的第一个元素。
示例:
lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title4', 'Ian', 'Ivy', 'Lu', 'Victor']]
应该是:
lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title4', 'Ian', 'Ivy', 'Lu', 'Eva']]
我的方法是从Anna开始,在下一个列表中计算她的所有匹配,并保存列表中的匹配项,其中列表的长度为多少。然后对Tim进行相同的操作...在此示例中,第一个Matches_count列表的结果应为:[1,2,1]。这意味着“ title3”的列表应遵循下一步。然后更改订单,并使用以下以下列表执行相同的步骤。
到目前为止,我一直在尝试找到与此代码的数字匹配(在弄清楚这一点之后,更改订单将是下一步):
for i in range(len(lst)-1):
matches_count = [0] * len(lst[i+1:])
for j in range(1, len(lst[i])):
for k in range(len(lst)-i-1):
nextList = lst[k+1]
currentName = lst[i][j]
if currentName in nextList:
matches_count[k] = matches_count[k] + 1
print(matches_count)
第一个列表是正确的,其余的似乎是不正确的。这是:
[1, 2, 1]
[6, 1]
[1]
I want to change the order of the lists in a list depending on the matches of each element in the lists and also depending on how many matches there are. So if a string occurs in two lists, they should be ordered after one another. The more matches there are, the higher the priority for them to be after one another.
However, the first element of each list should not be considered in that ordering.
Example:
lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title4', 'Ian', 'Ivy', 'Lu', 'Victor']]
should be:
lst = [['title1', 'Anna', 'Tim', 'Bob', 'Victor'], ['title3', 'Anna', 'Tim', 'Kate', 'Ruth'], ['title2', 'Kate', 'Lisa', 'Pam', 'Bob', 'Eva', 'Max'], ['title4', 'Ian', 'Ivy', 'Lu', 'Eva']]
My approach is to start with Anna, count all her matches in the next lists and save the number of matches in a list with the length of how many lists follow. Then do the same with Tim... In this example the result of the first matches_count list should be: [1, 2, 1]. That means the list with 'title3' should follow next. Then changing the order and do these same steps with the following list that was put there previously.
Until now, I was trying to find the number matches with this code (changing of the order would be the next step, after figuring this out):
for i in range(len(lst)-1):
matches_count = [0] * len(lst[i+1:])
for j in range(1, len(lst[i])):
for k in range(len(lst)-i-1):
nextList = lst[k+1]
currentName = lst[i][j]
if currentName in nextList:
matches_count[k] = matches_count[k] + 1
print(matches_count)
The first list is right, the rest does not seem to be correct. It is:
[1, 2, 1]
[6, 1]
[1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论