Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python遍历列表并将项目重新拆分为列表_Python_Python 3.x_List_Split - Fatal编程技术网

Python遍历列表并将项目重新拆分为列表

Python遍历列表并将项目重新拆分为列表,python,python-3.x,list,split,Python,Python 3.x,List,Split,我有一个列表,其中包含我想再次拆分并添加为列表中新项目的项目。例如,给出以下列表: pre_songs = ['Totem', 'One, Two, Three', 'Rent', 'Vapors', 'Get Loud > Inspire Strikes Back', 'Enceladus', 'Moon Socket', 'Out of This World > Scheme', 'Walk to the Light', '

我有一个列表,其中包含我想再次拆分并添加为列表中新项目的项目。例如,给出以下列表:

pre_songs = ['Totem', 'One, Two, Three', 'Rent', 'Vapors', 'Get Loud > Inspire Strikes Back', 
             'Enceladus', 'Moon Socket', 'Out of This World > Scheme', 'Walk to the Light', 
             'When The Dust Settles', 'Click Lang Echo']
我想将“Get Loud>Inspire Strokes Back”和“Out of the World>Scheme”项目按“>”进行拆分,并将“Get Loud”、“Inspire Strokes Back”、“Out of the World”和“Scheme”作为列表中的单独项目

我尝试使用下面的代码,但不起作用:

pre_setlist = []

for song in pre_songs:
    if song.contains('>'):
        pre_setlist.append(song.split('>'))
    else:
        pre_setlist.append(song)
这是一种方式

from itertools import chain

pre_setlist = []

for song in pre_songs:
    if '>' in song:
        pre_setlist.append(song.split(' > '))
    else:
        pre_setlist.append([song])

list(chain.from_iterable(pre_setlist))

# ['Totem', 'One, Two, Three', 'Rent', 'Vapors', 'Get Loud',
#  'Inspire Strikes Back', 'Enceladus', 'Moon Socket',
#  'Out of This World', 'Scheme', 'Walk to the Light',
#  'When The Dust Settles', 'Click Lang Echo']
这可以更简洁地写为列表理解:

from itertools import chain

pre_setlist = [[song] if '>' not in song \
               else song.split(' > ') for song in pre_songs]

list(chain.from_iterable(pre_setlist))

使用
扩展

pre_setlist = []
for song in pre_songs:
    pre_setlist.extend([x.strip() for x in song.split('>')])
如果
>
前后始终有一个空格,则更短:

pre_setlist = []
for song in pre_songs:
    pre_setlist.extend(song.split(' > '))
两个版本的结果以及示例列表:

>>> pre_setlist
['Totem',
 'One, Two, Three',
 'Rent',
 'Vapors',
 'Get Loud',
 'Inspire Strikes Back',
 'Enceladus',
 'Moon Socket',
 'Out of This World',
 'Scheme',
 'Walk to the Light',
 'When The Dust Settles',
 'Click Lang Echo']
您可以尝试以下方法:

pre_songs = ['Totem', 'One, Two, Three', 'Rent', 'Vapors', 'Get Loud > Inspire Strikes Back', 
         'Enceladus', 'Moon Socket', 'Out of This World > Scheme', 'Walk to the Light', 
         'When The Dust Settles', 'Click Lang Echo']
final_songs = [i for b in [c.split(' > ') for c in pre_songs] for i in b]
输出:

['Totem', 'One, Two, Three', 'Rent', 'Vapors', 'Get Loud', 'Inspire Strikes Back', 'Enceladus', 'Moon Socket', 'Out of This World', 'Scheme', 'Walk to the Light', 'When The Dust Settles', 'Click Lang Echo']
你需要改变两件事

  • 歌曲中的
    '>'而不是
    歌曲。包含('>')
  • 使用
    extend
    而不是
    append
    ,因为
    song.split
    会返回一个列表

  • “但它不起作用”什么不起作用?AttributeError:“str”对象没有属性“contains”好的,你可以在这里回答为什么它不起作用xD我的意思是你可以用谷歌搜索它。你应该总是在你的问题中包含完整的回溯,格式为代码。
    for song in pre_songs:
        if '>' in song:
            pre_setlist.extend(song.split('>'))
        else:
            pre_setlist.append(song)