简体   繁体   中英

How to remove thousand separator in a list of tuple?

I have a master_list which contains a list of lists, I'm trying to turn it into a list of tuples that only contains line[3]: the numbers of games played, and line[0]: the players' names. The list will be sorted on games played (highest to lowest) and only the top ten are returned, ie the list contains ten tuples in decreasing, sorted order. And I must remove the comma and convert to an integer before sorting

The output of my code is correct except the thousand separators are still there. This is my code:

def games_played(master_list):
container2 = []
for line in master_list:
    try:
        int(line[3])
        continue
    except ValueError:
        for i in line[3]:
            i = i.replace(",", "")
            continue
    container2.append(((line[3]), line[0]))
container2.sort(reverse = True)
container2 = container2[:10]
print(container2)

and this is my output:

[
 ('1,767', 'Gordie Howe'),
 ('1,756', 'Mark Messier'),
 ('1,733', 'Jaromir Jagr'),
 ('1,731', 'Ron Francis'),
 ('1,514', 'Steve Yzerman'),
 ('1,487', 'Wayne Gretzky'),
 ('1,451', 'Teemu Selanne'),
 ('1,409', 'Paul Coffey'),
 ('1,396', 'Stan Mikita'),
 ('1,378', 'Joe Sakic')
]

只需添加行(在最后一次分配给 container2 之后):

container2 = list(map(lambda x: (int(x[0].replace(',','')),x[1]),container2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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