I need help in splitting by coma a content of a list that is inside a list, basically I have such 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']]
And I want this outcome
[[['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']]]
CodePudding user response:
Try:
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)
Prints:
[
[
[
"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",
],
],
]
CodePudding user response:
There are two ways. Using a nested list comprehension:
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]
or using a for
loop:
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
CodePudding user response:
This can easily be done by using the str.split(substr)
method. In this case, it would look like this:
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]
CodePudding user response:
Using split to transform string -> list for each string item.
Below code for references
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)