英文:
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:]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论