【问题标题】:How to change elements in my list? My list splits on ',' while I want my list to split on ';'如何更改列表中的元素?我的列表在 ',' 上拆分,而我希望我的列表在 ';' 上拆分
【发布时间】:2020-05-24 13:32:22
【问题描述】:

现在我有以下列表:

row_2 = ['23-05-2020 14:28;World of Warcraft/Guild Wars/EVE Online (MMORPG);Counter Strike/Call of Duty: Modern Warfare (FPS games);Fortnite (Battle Royale games);FIFA (sport games);Red dead Redemption 2/Spider-Man (Adventure games);Man;18-25;7-12 uren per week;\x7fVrijheid: Ik wil mijn eigen acties en keuzes maken. Ik wil autonomie hebben.',
' \x7fUitdaging: Ik wil mij competitief en effectief voelen.',
' \x7fVerbinden: Ik wil mij verbinden met anderen. Multiplayer games zijn daarom belangrijk.;Uitdaging;Verbinden;Vrijheid;3;4;4;2;3;4;4;2;4;2;3;1;5;2;2;3;2;2;4;4;2;4;4;4;4;4;4;1;1;4;2;3;4;2;4;2;4;3;1;5;4;3;2;2;2;4;4;4;2']

当我打印列表的元素时,我得到以下 3 个元素作为输出:

23-05-2020 14:28;World of Warcraft/Guild Wars/EVE Online (MMORPG);Counter Strike/Call of Duty: Modern Warfare (FPS games);Fortnite (Battle Royale games);FIFA (sport games);Red dead Redemption 2/Spider-Man (Adventure games);Man;18-25;7-12 uren per week;Vrijheid: Ik wil mijn eigen acties en keuzes maken. Ik wil autonomie hebben.

Uitdaging: Ik wil mij competitief en effectief voelen.

Verbinden: Ik wil mij verbinden met anderen. Multiplayer games zijn daarom belangrijk.;Uitdaging;Verbinden;Vrijheid;3;4;4;2;3;4;4;2;4;2;3;1;5;2;2;3;2;2;4;4;2;4;4;4;4;4;4;1;1;4;2;3;4;2;4;2;4;3;1;5;4;3;2;2;2;4;4;4;2

但是,我不想将这些项目作为列表的元素,我不想将元素拆分为“,”,而是拆分为“;”。我的输出需要如下所示:

['23-05-2020 14:28', 'World of Warcraft/Guild Wars/EVE Online (MMORPG)', Counter Strike/Call of Duty: Modern Warfare (FPS games)', Fortnite (Battle Royale games)', ...] **And so on**

我怎样才能得到我想要的输出?

【问题讨论】:

    标签: python list split element


    【解决方案1】:

    使用列表推导:

    [x for s in l for x in s.split(';')]
    

    【讨论】:

      【解决方案2】:

      假设您的字符串如下所示,您可以使用 ';' 进行拆分如下图:

      myString = ('23-05-2020 14:28;World of Warcraft/Guild Wars/EVE Online '
                  '(MMORPG);Counter Strike/Call of Duty: Modern Warfare (FPS games);'
                  'Fortnite (Battle Royale games);FIFA (sport games);Red dead '
                  'Redemption 2/Spider-Man (Adventure games);Man;18-25;7-12 uren '
                  'per week;\x7fVrijheid: Ik wil mijn eigen acties en keuzes maken.'
                  ' Ik wil autonomie hebben., \x7fUitdaging: Ik wil mij competitief '
                  'en effectief voelen., \x7fVerbinden: Ik wil mij verbinden met '
                  'anderen. Multiplayer games zijn daarom belangrijk.;Uitdaging;'
                  'Verbinden;Vrijheid;3;4;4;2;3;4;4;2;4;2;3;1;5;2;2;3;2;2;4;4;2;'
                  '4;4;4;4;4;4;1;1;4;2;3;4;2;4;2;4;3;1;5;4;3;2;2;2;4;4;4;2')
      
      myList = myString.split(';')
      print(myList)
      

      我得到以下输出:

      ['23-05-2020 14:28', 'World of Warcraft/Guild Wars/EVE Online (MMORPG)', 'Counter Strike/Call of Duty: Modern Warfare (FPS games)', 'Fortnite (Battle Royale games)', 'FIFA (sport games)', 'Red dead Redemption 2/Spider-Man (Adventure games)', 'Man', '18-25', '7-12 uren per week', '\x7fVrijheid: Ik wil mijn eigen acties en keuzes maken. Ik wil autonomie hebben., \x7fUitdaging: Ik wil mij competitief en effectief voelen., \x7fVerbinden: Ik wil mij verbinden met anderen. Multiplayer games zijn daarom belangrijk.', 'Uitdaging', 'Verbinden', 'Vrijheid', '3', '4', '4', '2', '3', '4', '4', '2', '4', '2', '3', '1', '5', '2', '2', '3', '2', '2', '4', '4', '2', '4', '4', '4', '4', '4', '4', '1', '1', '4', '2', '3', '4', '2', '4', '2', '4', '3', '1', '5', '4', '3', '2', '2', '2', '4', '4', '4', '2']
      

      【讨论】:

        【解决方案3】:

        要获得输出,您必须使用split 字符串方法,如下所示,

        s="23-05-2020 14:28;World of Warcraft/Guild Wars/EVE Online (MMORPG);Counter Strike/Call of Duty: Modern Warfare (FPS games);Fortnite (Battle Royale games);FIFA (sport games);Red dead Redemption 2/Spider-Man (Adventure games);Man;18-25;7-12 uren per week;Vrijheid: Ik wil mijn eigen acties en keuzes maken. Ik wil autonomie hebben."
        
        l1=list(s.split(";")) #splits the string on ";"
        
        print(l1)
        

        输出:

        ['23-05-2020 14:28', 'World of Warcraft/Guild Wars/EVE Online (MMORPG)', 'Counter Strike/Call of Duty: Modern Warfare (FPS games)', 'Fortnite (Battle Royale games)', 'FIFA (sport games)', 'Red dead Redemption 2/Spider-Man (Adventure games)', 'Man', '18-25', '7-12 uren per week', 'Vrijheid: Ik wil mijn eigen acties en keuzes maken. Ik wil autonomie hebben.']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 2021-11-22
          • 1970-01-01
          • 2021-07-04
          • 2018-05-19
          • 2017-03-14
          相关资源
          最近更新 更多