简体   繁体   中英

Trying to pull parts of each item from a list into a new list

I have a long list (called "CurrentTitleAndCompany") that looks like this:

['VP of Human Resources at Arkansas Glass Container Corp · 2019 – Present', 'Compensation Benefits Administrator at Confederated Tribes of Warm Springs, Oregon · 2018 – Present', 'Vice President Employee Benefits at USI Insurance Services · 2020 – Present', 'Benefits Team Sub-Lead at ASM Research · 2019 – Present',]

I want to create a new list (called "CurrentTitle") that takes the parts from each item that come before "at".

This is what I wrote, but I'm getting a "list index out of range" error:

CurrentTitle = []

for element in CurrentTitleAndCompany:
    CurrentTitle.append(element.split("at",0)[1])

My personal suggestion would be for you to create a class and separate the titles from the companies, such as the following:

class CurrentTitleAndCompany:
    CompanyPersonOne = "Apple"
    TitlePersonOne = "CEO"
    CompanyPersonTwo = "Microsoft"
    TitlePersonTwo = "Vice President"


print("Person One currently works at", CurrentTitleAndCompany.CompanyPersonOne, 
"and occupies the title of ",
      CurrentTitleAndCompany.TitlePersonOne)

print("Person Two currently works at", CurrentTitleAndCompany.CompanyPersonTwo, 
"and occupies the title of ",
      CurrentTitleAndCompany.TitlePersonTwo)

If you need any other sort of clarification, hit me up!

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