简体   繁体   中英

Splitting a list into sublist:

I have scraped an Inmate search website, and there are instances of an inmate having multiple charges per arrest. I plan to map them in a one-to-many architecture database in django. However, whenever I scrape, the instances where an inmate has multiple charges result look like this:

[['MURDER   /   Disposed   /   $35,000.00', 'MANUF., POSS OF OTHER SUB. IN SCH. I,IIIII OR PWID - 1ST OFFENSE   /   Disposed   /   $15,000.00']...]

Thus, instead of being one too many, it is one-to-one. The two charges are separated by a comma following the 35,000. Does anyone have any ideas if this is possible, for there are some instances of 10+ charges per inmate?

Note, whenever I brought in the data, I split on new lines as such:

charges = []
charges_info = driver.find_elements_by_class_name("clear-cell-border")
for p in range(len(charges_info)):
    charges.append(charges_info[p].text.strip().split('\n'))

Thanks!

Since its a list in a list, you'll need to unpack it twice

Non list-comprehension

charges_info = [['MURDER   /   Disposed   /   $35,000.00', 'MANUF., POSS OF OTHER SUB. IN SCH. I,IIIII OR PWID - 1ST OFFENSE   /   Disposed   /   $15,000.00']]

new_list = []
for charges in charges_info:
    for i in charges:
        new_list.append(i)

for item in new_list:
    print(item)

>>> MURDER   /   Disposed   /   $35,000.00
>>> MANUF., POSS OF OTHER SUB. IN SCH. I,IIIII OR PWID - 1ST OFFENSE   /   Disposed   /   $15,000.00

List-comprehension

charges_info = [['MURDER   /   Disposed   /   $35,000.00', 'MANUF., POSS OF OTHER SUB. IN SCH. I,IIIII OR PWID - 1ST OFFENSE   /   Disposed   /   $15,000.00']]

new_list = [i for charges in charges_info for i in charges]

for item in new_list:
    print(item)

>>> MURDER   /   Disposed   /   $35,000.00
>>> MANUF., POSS OF OTHER SUB. IN SCH. I,IIIII OR PWID - 1ST OFFENSE   /   Disposed   /   $15,000.00

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