首页 > 解决方案 > I'm trying to remove information at the beginning and end of a list, but some of the uppercase letters mess up the code

问题描述

Below is the aforementioned list.

memberList = ['uuid_e04a043abc334bd1a2fbd167bdce1673[MVP+] IgrisGuild Master2020/07/21 '
 '02:35:052020/08/09 01:52:37',
 'uuid_1f12bce8313040a7978d5c51ceb9d82d[VIP] mistercintPrince2020/08/01 '
 '00:31:342020/08/08 23:47:53',
 'uuid_405e46954f804487ae9c18689f0c351b[MVP+] zoucePrince2020/08/06 '
 '20:11:222020/08/08 22:02:04',
 'uuid_a2b224ba7c5d42ee8d46b2c08297cef5 viellythedivelonBaron2020/07/25 '
 '02:54:022020/08/08 22:53:56',
 'uuid_ac8f62b779624750ad287320f2505cea[MVP+] Louis7864Baron2020/07/26 '
 '19:04:422020/08/09 02:22:07',
 'uuid_8a350042ed6a474ba4b186e6126e0be1[VIP] Broadside1138Baron2020/07/29 '
 '04:07:072020/08/09 03:01:48',
 'uuid_031c3178bfd04099b34301185d1182f3[VIP] KuttaBaron2020/07/31 '
 '02:15:202020/08/09 03:16:53',
 'uuid_63f8371a69bc404d855afd61f5775db2[VIP+] BabaloopsBaron2020/08/06 '
 '23:34:452020/08/08 22:52:44',
 'uuid_559b3816586c4db5909fb0ca4f2b56e8[VIP+] SparkleDuck9Baron2020/08/08 '
 '23:17:132020/08/09 02:12:58',
 'uuid_c469d1cbc4344a36a110664fdc1ba571[VIP] TooLongOfAUserNaNoble2020/08/01 '
 '03:54:592020/08/09 02:57:22']

This line removes part of it that is universal to every single object in the list.

memberList = [e[37:-38] for e in memberList]

The last two lines trim off the rest, but for some, it trims too much

members = [re.sub( r"([A-Z])", r" \1", member.split(" ")[1]).split()[0] for member in memberList]
print(members)

The output I want would look something like this:

['Igris', 'mistercint', 'zouce', 'viellythedivelon', 'Louis7864', 'Broadside138', 'Kutta', 'Babaloops', SparkleDuck9, 'TooLongOfAUserNa']

Thanks for your consideration, I am very new to this platform and any help would be appreciated.

标签: pythonlist

解决方案


拆分后,您希望将最后一个大写字符删除到字符串末尾。

members = [re.sub("[A-Z][^A-Z]+$", "", member.split(" ")[1]) for member in memberList]

然后你得到

['Igris', 'mistercint', 'zouce', 'viellythedivelon', 'Louis7864', 'Broadside1138', 'Kutta', 'Babaloops', 'SparkleDuck9', 'TooLongOfAUserNa']

推荐阅读