Python 我想要一个像这样的输出:“quot;粘贴R2的文件最多,有32个文件。”;使用函数

Python 我想要一个像这样的输出:“quot;粘贴R2的文件最多,有32个文件。”;使用函数,python,arrays,string,list,Python,Arrays,String,List,问题1:我想要一个如下输出:“粘贴R2的文件最多,有32个文件。” 我创建了一个函数,但只得到了最大值,我还需要相应的粘贴 data = [['R1', 28], ['R2', 32], ['R3', 1], ['L4', 0], ['L5', 10], ['L6', 22], ['L7', 30], ['L8', 19]] 其中,例如,R1是一个粘贴,28是该粘贴上的文件数 def max_past(a_list): list_of_index1 = [i[1] fo

问题1:我想要一个如下输出:“粘贴R2的文件最多,有32个文件。”

我创建了一个函数,但只得到了最大值,我还需要相应的粘贴

data = [['R1', 28], ['R2', 32], ['R3', 1], ['L4', 0],
        ['L5', 10], ['L6', 22], ['L7', 30], ['L8', 19]]
其中,例如,
R1
是一个粘贴,28是该粘贴上的文件数

def max_past(a_list):
    list_of_index1 = [i[1] for i in a_list]
    return max(list_of_index1)

print(f"Paste {data[0]}has the most files with {max_past(data)} files")

问题2:我如何使用一个函数,该函数将为我提供名称(R1、R2、R3)中带有
R
的粘贴文件数?

您可以使用以下方法找到最大值:


把R改成你想要的。根据此代码段创建函数。

问题1)您的“问题1”没有问题。问题2)你能澄清一下吗?您的
数据
已经提供了文件数量以及相应的
R
s。我的问题1是如何获得输出,因为我成功地返回了最大数量的文件,但还需要粘贴它们。关于第二个问题,我需要一个输入为字母(或“R”或“L”)的函数函数返回以该字母开头的所有粘贴中统计的文件总数。然后,找出哪种类型的粘贴(R或L)有更多的文件。你自己试过解决第二个问题吗?如果有,请将您的尝试包括在postI Get“索引器:字符串索引超出范围”def max_pass(a_list):list_of_index1=[i[1]for i in a_list]return max(list_of_index1)print(f“粘贴{data[0]}的文件最多,包含{max_pass(data)}文件”)我的问题在“{data[0]}”中,因为我确信它是错误的。我已经用这个函数得到了最大数量的os文件,但是我也想要粘贴这个数量的文件,你不需要编写你自己的函数。内置的
max
已经可以做到这一点,如我的答案所示
>>> max(data, key=lambda element: element[1])
['R2', 32]
>>> print(f"Paste {_[0]}has the most files with {_[1]} files")
Paste R2has the most files with 32 files
"""Question 1"""
data = [['R1', 28], ['R2', 32], ['R3', 1], ['L4', 0],
    ['L5', 10], ['L6', 22], ['L7', 30], ['L8', 19]]

data.sort(key= lambda val: val[1], reverse = True)

print(f"Paste {data[0][0]} has the most files with {data[0][1]} files")
#Paste R2 has the most files with 32 files

"""Question 2"""
new_list = []
for elem in data:
   if 'R' in elem[0]:
       new_list.append(elem)


#[['R2', 32], ['R1', 28], ['R3', 1]]