简体   繁体   English

查找列表中两个元素之间关系的最pythonic方法

[英]Most pythonic way to find relation between two elements in a list

Let's say I have the following list:假设我有以下列表:

l = ["watermelon", "banana", "orange", "apple"]

And I want to write a function that returns whatever the element is before another element in the list.我想编写一个函数,该函数返回列表中另一个元素之前的元素。 for example:例如:

>> is_before("banana", "watermelon")
>> False
>> is_before("banana", "apple")
>> True

What is the most pythonic way to write such a function?编写这样一个函数的最pythonic 的方法是什么?

You could do (assuming there are no duplicates):你可以这样做(假设没有重复):

l = ["watermelon", "banana", "orange", "apple"]

indeces = {w: i for i, w in enumerate(l)}

def is_previous(x, y):
    return indeces[x] < indeces[y]

>>> is_previous("banana", "watermelon")
False
>>> is_previous("banana", "apple")
True

This does not handle the case where any of the arguments aren't in the initial list.这不处理任何参数不在初始列表中的情况。

mylist = ["watermelon", "banana", "orange", "apple"]

def is_before(prev_item, target, arr):
    return prev_item in arr[:arr.index(target)]

>>>is_before("banana", "apple", mylist)
True
>>>is_before("banana", "watermelon", mylist)
False

If you want to handle duplicates you can use something like this如果你想处理重复,你可以使用这样的东西

def find_item_last_index(count, item, arr, index=0):
    # A recursive function for finding the last index of an item in a list
    if count == 1:
        return index + arr.index(item)
    return (find_item_last_index(count-1, item, arr[arr.index(item)+1:],  
                                 index+arr.index(item)+1))

def is_before(prev_item, target, arr):
    return prev_item in arr[: find_item_last_index(arr.count(target), target, arr)]

mylist =  ["watermelon", "apple", "banana", "orange", "apple"]

>>>is_before("banana", "apple", mylist)
True
>>>is_before("banana", "watermelon", mylist)
False

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

相关问题 在列表中找到与其他元素不同的元素的最pythonic方法是什么? - what is most pythonic way to find a element in a list that is different with other elements? 一种pythonic方法,用于查找值是否在列表中的两个值之间 - A pythonic way how to find if a value is between two values in a list 大多数pythonic方式找到np.array列表中的最大元素? - Most pythonic way to find the maximum elements out of list of np.array? 什么是确保列表中所有元素不同的最pythonic方法? - What's the most pythonic way to ensure that all elements of a list are different? 排除以特定字符开头的列表元素的最pythonic方法是什么? - What is the most pythonic way to exclude elements of a list that start with a specific character? 在两个值之间切换变量的大多数Python方法 - Most pythonic way to toggle a variable between two values 删除两个定界符之间的文本的大多数pythonic方法 - Most pythonic way to delete text between two delimiters 大多数Pythonic方法在O(1)复杂度的列表中查找/检查项目? - Most Pythonic way to find/check items in a list with O(1) complexity? 在列表列表中查找所有无的最 Pythonic 方法是什么? - What is the most Pythonic way to find all Nones in a list of lists? Pythonic方法获取字典中的所有元素,落在两个键之间? - Pythonic way to fetch all elements in a dictionary, falling between two keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM