簡體   English   中英

如何測試哪個子列表包含特定元素,然后打印該子列表中的元素

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

文件名: records.csv

示例文件內容:

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()

就像上面的注釋中提到的那樣,列表“info”由子列表組成,每個子列表都包含來自“records.csv”文本文件的一行。 所以第一個子列表,例如是['11', 'Owen', 17]。 我已經從所有子列表中找到了最大“點”,在這種情況下為 35,並且希望能夠識別包含該元素的子列表,然后從所述子列表中打印元素。 任何幫助表示贊賞,謝謝。

已更新您的代碼以執行所需的操作

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()

如果您只需要一個最小值或最大值,那么對列表進行排序然后獲得您需要的值是最便宜的。

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])

我在寫邏輯,你可以把它放到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:

Top scorer:  Ryan, Points  35

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM