用逗号分割嵌入列表的python列表的内容

Split by coma the content of python list embedded in list

我需要帮助通过逗号拆分列表中的列表内容,基本上我有这样的数据

[['Department of Computer Languages and Computing Sciences, University of Málaga, Málaga, Spain'],
 ['Poste Italiane - Information Technology, Research and Development - R and D Center, Piazza Matteotti 3, 80133 Naples, Italy'],
 ['Department of Computer and Information Science, University of Macau, Macau',
  'Mathematics and Scientific Computing, National Physical Laboratory, Teddington, London TW11 0LW, United Kingdom',
  'Department of Computer Science and Engineering, C. V. Raman College of Engineering, Bhubaneswar 752054, India']]

我想要这个结果

[[['Department of Computer Languages and Computing Sciences',
   'University of Málaga',
   'Málaga',
   'Spain']],
 [['Poste Italiane - Information Technology',
   'Research and Development - R and D Center',
   'Piazza Matteotti 3',
   '80133 Naples',
   'Italy']],
 [['Department of Computer and Information Science',
   'University of Macau',
   'Macau'],
  ['Mathematics and Scientific Computing',
   'National Physical Laboratory',
   'Teddington',
   'London TW11 0LW',
   'United Kingdom'],
  ['Department of Computer Science and Engineering',
   'C. V. Raman College of Engineering',
   'Bhubaneswar 752054',
   'India']]]

尝试:

lst = [
    [
        "Department of Computer Languages and Computing Sciences, University of Málaga, Málaga, Spain"
    ],
    [
        "Poste Italiane - Information Technology, Research and Development - R and D Center, Piazza Matteotti 3, 80133 Naples, Italy"
    ],
    [
        "Department of Computer and Information Science, University of Macau, Macau",
        "Mathematics and Scientific Computing, National Physical Laboratory, Teddington, London TW11 0LW, United Kingdom",
        "Department of Computer Science and Engineering, C. V. Raman College of Engineering, Bhubaneswar 752054, India",
    ],
]

out = [[[*map(str.strip, s.split(","))] for s in sublist] for sublist in lst]
print(out)

打印:

[
    [
        [
            "Department of Computer Languages and Computing Sciences",
            "University of Málaga",
            "Málaga",
            "Spain",
        ]
    ],
    [
        [
            "Poste Italiane - Information Technology",
            "Research and Development - R and D Center",
            "Piazza Matteotti 3",
            "80133 Naples",
            "Italy",
        ]
    ],
    [
        [
            "Department of Computer and Information Science",
            "University of Macau",
            "Macau",
        ],
        [
            "Mathematics and Scientific Computing",
            "National Physical Laboratory",
            "Teddington",
            "London TW11 0LW",
            "United Kingdom",
        ],
        [
            "Department of Computer Science and Engineering",
            "C. V. Raman College of Engineering",
            "Bhubaneswar 752054",
            "India",
        ],
    ],
]

有两种方法。使用嵌套列表理解:

text = [['Department of Computer Languages and Computing Sciences, University of Málaga, Málaga, Spain'],
 ['Poste Italiane - Information Technology, Research and Development - R and D Center, Piazza Matteotti 3, 80133 Naples, Italy'],
 ['Department of Computer and Information Science, University of Macau, Macau',
  'Mathematics and Scientific Computing, National Physical Laboratory, Teddington, London TW11 0LW, United Kingdom',
  'Department of Computer Science and Engineering, C. V. Raman College of Engineering, Bhubaneswar 752054, India']]


[[x.split(',') for x in y] for y in text]

或使用 for 循环:

text = [['Department of Computer Languages and Computing Sciences, University of Málaga, Málaga, Spain'],
 ['Poste Italiane - Information Technology, Research and Development - R and D Center, Piazza Matteotti 3, 80133 Naples, Italy'],
 ['Department of Computer and Information Science, University of Macau, Macau',
  'Mathematics and Scientific Computing, National Physical Laboratory, Teddington, London TW11 0LW, United Kingdom',
  'Department of Computer Science and Engineering, C. V. Raman College of Engineering, Bhubaneswar 752054, India']]

output = []
for y in text:
    temp = []
    for x in y:
        temp.append(x.split(','))
    output.append(temp)    
output

这可以使用 str.split(substr) 方法轻松完成。在这种情况下,它看起来像这样:

my_data = [
  ["field 1, filed 2, filed 3"],
  ["more data, data is cool, i like data"]]
new_data = [d[0].split(", ") for d in my_data]

使用 split 转换字符串 -> 列出每个字符串项。

以下代码供参考

data = [
        ['Department of Computer Languages and Computing Sciences, University of Málaga, Málaga, Spain'],
        ['Poste Italiane - Information Technology, Research and Development - R and D Center, Piazza Matteotti 3, 80133 Naples, Italy'],
        ['Department of Computer and Information Science, University of Macau, Macau',
         'Mathematics and Scientific Computing, National Physical Laboratory, Teddington, London TW11 0LW, United Kingdom',
         'Department of Computer Science and Engineering, C. V. Raman College of Engineering, Bhubaneswar 752054, India']
]

new_data = list(map(lambda item: list(map(lambda subitem: subitem.split(', '), item)), data))

print(new_data)