我需要帮助用昏迷来分割列表中的一个内容,基本上我有这样的数据。
[['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']]]发布于 2022-04-07 21:41:29
尝试:
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",
],
],
]发布于 2022-04-07 21:43:04
有两种方法。使用嵌套列表理解:
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发布于 2022-04-07 21:44:46
通过使用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]https://stackoverflow.com/questions/71789195
复制相似问题