Python 比较列表中元素的最后第6个字符

Python 比较列表中元素的最后第6个字符,python,Python,Python是个新手 我目前正在尝试比较对目录中的文件执行glob.glob方法的返回值,但在理解如何比较未知列表中每个元素末尾的第6个字符时遇到了障碍 例如,output=glob.glob('Report1.0REV.*.xlsx') 会回来吗 ['Report1.0REV_A.xlsx', 'Report1.0REV_B.xlsx', 'Report1.0REV_C.xlsx', etc] 但是,如果我附加每个文件以获得A>B、C>B或X>Y,然后打印最新的文件,我将如何比较 到目前为止

Python是个新手

我目前正在尝试比较对目录中的文件执行glob.glob方法的返回值,但在理解如何比较未知列表中每个元素末尾的第6个字符时遇到了障碍

例如,
output=glob.glob('Report1.0REV.*.xlsx')
会回来吗

['Report1.0REV_A.xlsx', 'Report1.0REV_B.xlsx', 'Report1.0REV_C.xlsx', etc]
但是,如果我附加每个文件以获得A>B、C>B或X>Y,然后打印最新的文件,我将如何比较

到目前为止,我能想到的是用列表的长度写一个for循环,但我一直在理解如何将列表中每个元素的-6字符与哪个更大进行比较,然后打印该文件

for i in range(len(Output)):

感谢您的阅读和帮助

如果我正确理解了您的答案,您希望在对列表排序后获取最后一个文件名。如果是这种情况,那么下面的代码可能会帮助您。正如您所说,您是Python新手,我添加了大量的注释,这些注释可能会有所帮助:

# Let's first create a list of unsorted data.
# In this case, (A, C, B) are not in lexicographic order.
unsorted_data = [
    'Report1.0REV_A.xlsx',
    'Report1.0REV_C.xlsx',
    'Report1.0REV_B.xlsx',
]


# The function "pick_letter" will be used later on. It is a very simple
# function comprised of only one statement. That is a very good candidate for a
# "lambda expression". But as you say that you are new to Python, I'll give two
# examples below. One using "lambda", the other using the function
# "pick_letter".
def pick_letter(value):
    '''
    This function takes a value and returns the 6th item from the end. The
    value passed can be anythin that can be indexed, like strings or lists.
    '''
    return value[-6]


# Sorting the input data using "pick_letter"
#
# "sorted" takes an optional argument "key" which holds a function. This
# function will be called for each element in the list and must return
# "something". The list will returned by this "something". In your case, you
# want the 6th element from the end, so we use "pick_letter".
sorted_data_1 = sorted(unsorted_data, key=pick_letter)


# As mentioned above, the same can be achieved by a "lambda" expression. This
# saves us the definition of the "pick_letter" function. Using "lambdas" is
# debatable. They have no name, no doc-string and may be harder to understand.
# But this case is *so* simple that it really is a prime candidate for a
# lambda:
sorted_data_2 = sorted(unsorted_data, key=lambda x: x[-6])


print('Unsorted data:', unsorted_data)
print('Sorted data (using pick_letter):', sorted_data_1)
print('Sorted data (using lambda):', sorted_data_2)

# Now that it is sorted, you can pick the last element using [-1]:
print('Last element:', sorted_data_1[-1])


# Apart from sorting, we can create a new list containing only the 6th
# character from the end for each element. The code below is a so called
# "list comprehension". It is a Python feature which allows you to quickly
# create lists (and other similar collections) from other iterables. In
# this case, the list will have the same ordering as the input 
# (unsorted_data)
characters = [filename[-6] for filename in unsorted_data]
print('Characters:', characters)

你想按最后第六个字符排序吗?是的,获取最后6个字符很容易,只需使用
x[-6]
其中x是字符串。只需对列表排序。最后一个元素是最大的。罗伯特,你想澄清一下吗?如果你不澄清任何疑问,你不能指望我们理解你想要什么。我同意@cᴏʟᴅsᴘᴇᴇᴅ 在这里,总体目标是什么?要打印比较中较大的文件,还是要打印iterable中最大的文件?不清楚您要做什么。请看。明确地说明您期望的输出通常是好的。