简体   繁体   中英

How to remove strings ending with certain pattern from list?

I have the following code snippet:

result = {
             "cast": [],
         } | {k: get_plist_names(v["dict"]) for k, v in zipped}
cast_value = (', '.join(result.get("cast", None)))

Now sometimes I have a very long cast list, that often ends with eg "Ted Rai..." this is always the last entry in the list. Seems that somebody has cut off the 255 characters. How can I remove the last entry from my list if it ends with "..." ?

I use ', ' as separator between my strings.

If needed:

def get_plist_names(name_dict):
    return [o["string"] for o in (name_dict if isinstance(name_dict, list) else [name_dict])]

在调用join()时使用带有if条件的生成器。

cast_value = (', '.join(r for r in result.get("cast", None) if not r.endswith("...")))

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