This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers Interesting that it's not list.find , but list.index : >>> l = ['a', 'b', 'c'] >>> l.index('c') 2 To test for membership: >>> 'b' in l True Which is equivalent to (and should be used instead of): >>> l.__contains__('b') True
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 有趣的是,它不是list.find ,而是list.index : >>> l = ['a', 'b', 'c'] >>> l.index('c') 2 测试会员资格: >>> 'b' in l True 相当于(并且应该用来代替): >>> l.__contains__('b') True
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers Of course there is. Try >>> ['cow', 'dog', 'cat'].index('cow') 0 May i suggest, don't use list as a variable name, as it is already used for constructing a list object.
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 当然有。 尝试 >>> ['cow', 'dog', 'cat'].index('cow') 0 我可以建议,不要使用list作为变量名称,因为它已经用于构建列表对象。
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers Use the index() method on list. See Finding the index of an item given a list containing it in Python: >>> ["foo","bar","baz"].index('bar') 1 index(arg) returns the index of arg within a list. So you would do: from random import shuffle deck = [value + suit for
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 使用列表中的index()方法。 请参阅在Python中查找包含它的列表的项目索引: >>> ["foo","bar","baz"].index('bar') 1 index(arg)返回列表中arg的索引。 所以你会这样做: from random import shuffle deck = [value + suit for value in range(1, 11) + list ("AJQK") for suit in "HCDS"] + ["J1", "J2"] shuffle(deck) idx =
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers 尝试这个: >>> list_1=['a', 'b', 'c', 'd'] >>> list_2 = ['1', 'e', '1', 'e'] >>> index_list = ['1', '3'] >>> index_list = [int(i) for i in index_list] # convert str to int for index >>> list_1 = [i for n, i in enumerate(list_1) if n
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 尝试这个: >>> list_1=['a', 'b', 'c', 'd'] >>> list_2 = ['1', 'e', '1', 'e'] >>> index_list = ['1', '3'] >>> index_list = [int(i) for i in index_list] # convert str to int for index >>> list_1 = [i for n, i in enumerate(list_1) if n not in index_list] >>> list_2 = [i
This question already has an answer here: Python list of dictionaries search 15 answers Finding the index of an item given a list containing it in Python 23 answers
这个问题在这里已经有了答案: Python的词典列表搜索15个答案 在Python中找到一个包含它的列表的索引23个答案
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers 要避免一个循环,可以使用try-except子句(不使用if ... in ...: try: i = vocab.index(word) except ValueError as e: print e # Optional i = -1 This is a case where I wish I could do assignment in an if statement, but I can't so this is the solution: If vocab is a
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 要避免一个循环,可以使用try-except子句(不使用if ... in ...: try: i = vocab.index(word) except ValueError as e: print e # Optional i = -1 这是一个我希望我可以在if语句中进行赋值的情况,但我不能这样做,因为这是解决方案: 如果vocab是一个list : try: index = vocab.index(word) except ValueError: pass e
As a quick example: list1 = ['a', 'b', 'c'] list2 = ['a', 'stack', 'overflow'] for i in list1 and list2: print i this prints all the elements in list2 . Why is this? How can I just print the elements that are in both lists? If your lists can be big, its better to convert them to sets and use intersection over them: list1 = ['a', 'b', 'c'] list2 = ['a', 'stack', 'overflow'] for i in se
作为一个简单的例子: list1 = ['a', 'b', 'c'] list2 = ['a', 'stack', 'overflow'] for i in list1 and list2: print i 这会打印list2所有元素。 为什么是这样? 我怎样才能打印这两个列表中的元素? 如果您的列表可能很大,最好将它们转换为集合并使用它们的交集: list1 = ['a', 'b', 'c'] list2 = ['a', 'stack', 'overflow'] for i in set(list1).intersection(set(list2)): print i 如果您想重复地在该
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers 使用list.index : >>> TupList = [('ABC D','235'),('EFG H','462')] >>> TupList.index((u'EFG H',u'462')) 1 我认为你可以这样做TupList = [('ABC D','235'),('EFG H','462')] if ('ABC D','235') in TupList: print TupList.index(i)
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 使用list.index : >>> TupList = [('ABC D','235'),('EFG H','462')] >>> TupList.index((u'EFG H',u'462')) 1 我认为你可以这样做TupList = [('ABC D','235'),('EFG H','462')] if ('ABC D','235') in TupList: print TupList.index(i)
This question already has an answer here: Filter dataframe rows if value in column is in a set list of values 7 answers Finding the index of an item given a list containing it in Python 23 answers Use the Series.isin() method to check if a series value is in a list of values. In your case - df[df['House'].isin([1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58
这个问题在这里已经有了答案: 如果列中的值位于值集合列表中,则过滤数据框行7个答案 在Python中找到一个包含它的列表的索引23个答案 使用Series.isin()方法检查一系列值是否在值列表中。 在你的情况下 - df[df['House'].isin([1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156])] 示例 - In [77]: df Out[77]: A B 0 1 5 1 2 6 2 3 7 3 4
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers The best way is probably to use the list method .index. For the objects in the list, you can do something like: def __eq__(self, other): return self.Value == other.Value with any special processing you need. You can also use a for/in statement with enumerate(arr)
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 最好的方法可能是使用列表方法.index。 对于列表中的对象,您可以执行如下操作: def __eq__(self, other): return self.Value == other.Value 与您需要的任何特殊处理。 您还可以使用枚举(arr)的for / in语句 找到值> 100的项目索引的示例。 for index, item in enumerate(arr): if item > 100: return index,