简体   繁体   中英

Python: How to create a list by substrings there was splitted by another list of strings?

I need to create a list that will be composed by substrings extracted for another list of strings. I tried a "for-loop" code, but I get unexpected results.

For example:

description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
 'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]

descrition_splitted = []

for i in description:
  des = (i)
  descrition_splitted.append((des.split(',')))

The lenght of description_splitted list is just 2 elements instead of 9 (number of substrings). It is like that each element contains many substrings.

What I am doing wrong?

Is another "for-loop" needed?

You can use .extend instead of .append

descrition_splitted.extend((i.split(',')))

Or you can loop the result of the split on , again to get 9 results.

description = ['How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
               'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom',]

descrition_splitted = []

for i in description:
    for part in i.split(','):
        descrition_splitted.append(part)

print(len(descrition_splitted))

Output

9

You can use extend instead of append . This operates the same as the += mentioned in comments.

description = [
    'How g... and focus',
    'The s... chosen idiom'
]

comma_fragments = []

for segment in description:
  comma_fragments.extend(segment.split(','))

if you want to try a list comprehension, you can even do

comma_fragments = [s for d in description for s in d.split(',')]

eg

>>> description = ['a,b', 'c,d,e']
>>> [s for d in description for s in d.split(',')]
['a', 'b', 'c', 'd', 'e']

If I guess your purpose right, you need to use += (list + list or extend a list with a list) instead of append (insert item into list)

    descrition_splitted += des.split(',')

Btw, we can refactor your code as:

description = [
    'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
    'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]

descrition_splitted = []

for des in description:
    descrition_splitted += des.split(',')

Shorter:

description = [
    'How good can a bad trip ... Angels, bad karma can conjure ... impressive sounds, and on the bands ... album, 2017s Death Song, their moody... attack is executed, with an impressive... and focus',
    'The sitar-laced two ... sits, comfortably alongside ... desperation of "Doves," testament to the variety ... squeeze out of their chosen idiom', ]

descrition_splitted = []
list(map(descrition_splitted.extend, (des.split(',') for des in description)))

try this

descrition_splitted=[]
for i in description:
  descrition_splitted=descrition_splitted+i.split(',')
len(descrition_splitted)

output

9

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