如何遍历Python列表及其列表元素,在每个元素中拆分字符串并写出新列表

2024-05-28 22:55:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由数百个元素组成的列表,其中许多元素本身就是一个列表。我希望“展平”和重复列表,并将其写入文件。应写入展开列表的离散元素在源数据中用双反斜杠“\”分隔。例如,给出以下列表:

alist = ["ZZ Ward\\Eric Bell"],["ZZ Ward"],["Sabine Kabongo\\Salif Keita\\Cat Stevens\\Trilok Gurtu\\Lindsey Buckingham"], ["John Mellencamp\\Cat Stevens"]

我希望创建一个包含以下内容的新列表:

ZZ Ward
Eric Bell
Sabine Kabongo
Salif Keita
Cat Stevens
Trilok Gurtu
Lindsey Buckingham
John Mellencamp

作为Python新手,我做了很多搜索并尝试了各种方法,但还没有找到正确的解决方案

使用以下代码:

alist = ["ZZ Ward\\Eric Bell"],["ZZ Ward"],["Sabine Kabongo\\Salif Keita\\Cat Stevens\\Trilok Gurtu\\Lindsey Buckingham"], ["John Mellencamp\\Cat Stevens"]
alist = list(filter(None, alist))
alist.sort()
for list in alist:
  for element in list:
  print(type(element), element.split("\\"))

我已经设法做到了这一点:

<class 'str'> ['John Mellencamp', 'Cat Stevens']
<class 'str'> ['Sabine Kabongo', 'Salif Keita', 'Cat Stevens', 'Trilok Gurtu', 'Lindsey Buckingham']
<class 'str'> ['ZZ Ward']
<class 'str'> ['ZZ Ward', 'Eric Bell']

虽然重复数据消除应该相对简单,但如何将这些单独的条目转换为一个列表,而没有元素本身就是一个列表


Tags: 元素列表catericstevensbellwardzz
3条回答

列表理解是最好的方法,但如果你继续沿着你开始的道路走下去,我认为这会解决问题

alist = ["ZZ Ward\\Eric Bell"],["ZZ Ward"],["Sabine Kabongo\\Salif Keita\\Cat Stevens\\Trilok Gurtu\\Lindsey Buckingham"], ["John Mellencamp\\Cat Stevens"]
alist = list(filter(None, alist))
alist.sort()
for list in alist:
  for element in list:
      for item in element.split("\\"):
          print(type(item), item) 

首先,使用sum将列表展平

>>> sum(alist, [])
['ZZ Ward\\Eric Bell', 'ZZ Ward', 'Sabine Kabongo\\Salif Keita\\Cat Stevens\\Trilok Gurtu\\Lindsey Buckingham', 'John Mellencamp\\Cat Stevens']

然后,您可以使用相同的分隔符将所有列表条目连接成一个字符串:

>>> "\\".join(sum(alist, []))
'ZZ Ward\\Eric Bell\\ZZ Ward\\Sabine Kabongo\\Salif Keita\\Cat Stevens\\Trilok Gurtu\\Lindsey Buckingham\\John Mellencamp\\Cat Stevens'

完成后,可以使用分隔符拆分它们:

>>> "\\".join(sum(alist, [])).split("\\")
['ZZ Ward', 'Eric Bell', 'ZZ Ward', 'Sabine Kabongo', 'Salif Keita', 'Cat Stevens', 'Trilok Gurtu', 'Lindsey Buckingham', 'John Mellencamp', 'Cat Stevens']

现在,您可以使用set构造函数删除重复项,如前一个答案所示:

>>> set("\\".join(sum(alist, [])).split("\\"))
{'Trilok Gurtu', 'Sabine Kabongo', 'Lindsey Buckingham', 'Salif Keita', 'ZZ Ward', 'Eric Bell', 'Cat Stevens', 'John Mellencamp'}

因此,您可以看到,您想要的所有操作都可以在一行中完成

newlist = set("\\".join(sum(alist, [])).split("\\"))

您可以使用set尝试list comprehension

>>> new_list = [l3 for l1 in alist for l2 in l1 for l3 in l2.split('\\')]
>>> new_list
['ZZ Ward', 'Eric Bell', 'ZZ Ward', 'Sabine Kabongo', 'Salif Keita', 'Cat Stevens', 'Trilok Gurtu', 'Lindsey Buckingham', 'John Mellencamp', 'Cat Stevens']
>>> list(set(new_list))
['ZZ Ward', 'Salif Keita', 'Eric Bell', 'Lindsey Buckingham', 'Trilok Gurtu', 'Sabine Kabongo', 'John Mellencamp', 'Cat Stevens']
# If maintaining order is necessary:
>>> sorted(set(new_list), key=new_list.index)
['ZZ Ward', 'Eric Bell', 'Sabine Kabongo', 'Salif Keita', 'Cat Stevens', 'Trilok Gurtu', 'Lindsey Buckingham', 'John Mellencamp']

FWIW基本上是一个set comprehension就可以了,如果不需要订单:

>>> {l3 for l1 in alist for l2 in l1 for l3 in l2.split('\\')}
{'Cat Stevens',
 'Eric Bell',
 'John Mellencamp',
 'Lindsey Buckingham',
 'Sabine Kabongo',
 'Salif Keita',
 'Trilok Gurtu',
 'ZZ Ward'}

在传统版本中,可以这样做:

new_list = []
for l1 in alist:
    for l2 in l1:
        for l3 in l2.split('\\'):
            if l3 not in new_list:
                new_list.append(l3)
print(new_list)

输出:

['ZZ Ward', 'Eric Bell', 'Sabine Kabongo', 'Salif Keita', 'Cat Stevens', 'Trilok Gurtu', 'Lindsey Buckingham', 'John Mellencamp']

相关问题 更多 >

    热门问题