首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按昏迷拆分嵌入列表中python列表的内容

按昏迷拆分嵌入列表中python列表的内容
EN

Stack Overflow用户
提问于 2022-04-07 21:36:14
回答 4查看 44关注 0票数 1

我需要帮助用昏迷来分割列表中的一个内容,基本上我有这样的数据。

代码语言:javascript
复制
[['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']]

我想要这个结果

代码语言:javascript
复制
[[['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']]]
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-04-07 21:41:29

尝试:

代码语言:javascript
复制
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)

指纹:

代码语言:javascript
复制
[
    [
        [
            "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",
        ],
    ],
]
票数 2
EN

Stack Overflow用户

发布于 2022-04-07 21:43:04

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

代码语言:javascript
复制
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循环:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2022-04-07 21:44:46

通过使用str.split(substr)方法可以很容易地做到这一点。在这种情况下,应该是这样的:

代码语言:javascript
复制
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]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71789195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档