简体   繁体   English

如何测试哪个子列表包含特定元素,然后打印该子列表中的元素

[英]How to test which sublist contains a specific element, then print elements from that Sublist

File Name: records.csv文件名: records.csv

Sample file contents:示例文件内容:

11, Owen, 17

4, Hank, 18

77, Paul, 10

8, Ryan, 35

12, Patrick, 24
def getFileName():

    fileName = input('Input File Name: ')
    return fileName

def processFile(fileName):

    file = open(fileName, 'r')

    lines = file.readlines()

    fileList = []
    info = []
    pts = []

    for a in lines:
        fileList = a.split(',')
        fileList[-1] = int(fileList[-1])

        info.append(fileList)

    print('-----------------------------------------------')
    print('##\t Player\t             Points')
    print('-----------------------------------------------')

    for a in info:
        print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='')
    print('-----------------------------------------------')

    for a in info:
        pts.append(a[-1])

    maxPts = max(pts)


    # Find maxPts in one of the 5 sublist, and make a brand new list of it.


    print('Top Scorer:', 'Points:', maxPts)

    file.close()

def main():

    fileName = getFileName()
    processFile(fileName)

main()

Like mentioned in the note above, the list, 'info', is made up of sublists with each containing a line from the 'records.csv' text file.就像上面的注释中提到的那样,列表“info”由子列表组成,每个子列表都包含来自“records.csv”文本文件的一行。 So the first sublist, for example is ['11', ' Owen', 17].所以第一个子列表,例如是['11', 'Owen', 17]。 I already found the max 'points' from all the sublists, 35 in this case, and would like to be able to identify the sublist that contains that element, and then print elements from said sublist.我已经从所有子列表中找到了最大“点”,在这种情况下为 35,并且希望能够识别包含该元素的子列表,然后从所述子列表中打印元素。 Any help is appreciated, thank you.任何帮助表示赞赏,谢谢。

Have updated your code to do what is required已更新您的代码以执行所需的操作

def getFileName():

    fileName = input('Input File Name: ')
    return fileName

def processFile(fileName):

    file = open(fileName, 'r')

    lines = file.readlines()

    fileList = []
    info = []
    pts = []

    for a in lines:
        fileList = a.split(',')
        fileList[-1] = int(fileList[-1])

        info.append(fileList)

    print('-----------------------------------------------')
    print('##\t Player\t             Points')
    print('-----------------------------------------------')

    for a in info:
        print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='')
    print('-----------------------------------------------')

    for a in info:
        pts.append(a[-1])

    maxPts = max(pts)
    index=pts.index(maxPts) #this finds index of the element with higest score

    a=info[index] 
    print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='') #prints the list with higest score
    print


    # Find maxPts in one of the 5 sublist, and make a brand new list of it.


    print('Top Scorer:', 'Points:', maxPts)

    file.close()

def main():

    fileName = getFileName()
    processFile(fileName)

main()

If you only need a min or max value, it is cheapest to sort the list and then get the value that you need.如果您只需要一个最小值或最大值,那么对列表进行排序然后获得您需要的值是最便宜的。

info = [
  [0, "a", 121],
  [1, "aa", 14],
  [2, "b", 55]
]

print(sorted(info, key=lambda x:x[2])[0])
print(sorted(info, key=lambda x:x[2])[-1])

I am writing the logic, you can put it into function:我在写逻辑,你可以把它放到function中:

f=open('records.csv','r')
filecontent = f.readlines()
lists = [line.strip('\n').split(',') for line in filecontent if line.strip('\n')]
maxpts_sublst = max(lists,key = lambda x:int(x[-1]))
print("Top scorer: {0}, Points {1}".format(*maxpts_sublst[1:]))
f.close()

Output: Output:

Top scorer:  Ryan, Points  35

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM