使用类设置值来访问列表子集。

huangapple go评论62阅读模式
英文:

Using class set value to access a list subset

问题

使用compareList的值'brown'来打印testList的剩余部分,例如['fox', 'here!'],从结果中的foundList中。

foundList = testList[testList.index('brown') + 1:]
print(foundList)
英文:

The testList example represents list strings from a text file. The compareList represents a values list from a dictionary. I want to 'pull out' subsets of a string AFTER the match.

# Use a set result to print "fox here!"
testList = ['quick', 'brown', 'fox', 'here!']
compareList = ['beige', 'black', 'brown']
foundList = []

result = set(testList) & set(compareList)
# <class 'set'> {'brown'}

How to use the compareList value 'brown' to print the remainder of the testList eg ['fox', 'here!'] From a resultant foundList

答案1

得分: 1

获取brown的索引,然后切片列表的其余部分。

color = list(result)[0]
index = testList.index(color)
rest = testList[index+1:]
英文:

Get the index of brown and then slice the rest of the list.

color = list(result)[0]
index = testList.index(color)
rest = testList[index+1:]

huangapple
  • 本文由 发表于 2023年6月8日 06:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定