python - python程序获取一个字符串并返回一个列表而不使用像eva()这样的任何builin但是我们可以使用字符串内置函数吗
问题描述
我需要以下解决方案,但我无法获得解决方案 - 需要一个字符串返回一个列表
i/p: "[(694, 104), (153, 236), (201, 106), (601, 427)]"
o/p:
(694, 104)
(153, 236)
(201, 106)
(601, 427)
我写了下面的代码-
def convertor(string):
result = (string.split(" "))[0]
return result
string1 = "[(694, 104), (153, 236), (201, 106), (601, 427)]"
out=convertor(string1.replace("[","").replace("]",""))
print(out)
我得到的当前输出 --(694, 104), (153, 236), (201, 106), (601, 427) 但我需要上述格式的输出
解决方案
您可以添加.replace("), (", ")\n(")
如下:
def convertor(string):
result = (string.split("\n"))
return result
string1 = "[(694, 104), (153, 236), (201, 106), (601, 427)]"
out=convertor(string1.replace("[","").replace("]","").replace("), (", ")\n("))
for tpl in out:
print(tuple(tpl))
输出将是:
(694, 104)
(153, 236)
(201, 106)
(601, 427)
推荐阅读
- oracle-jet - 使用 JSON 数据绑定 Ojet 树视图
- flutter - Flutter Inherited Widget - 缺少一些监听器
- listview - 如何在 Nativescript-vue 中创建具有交错或网格布局的 ListView?
- unit-testing - 我们是否应该对谓词方法进行单元测试
- python - 气流任务无法连接到远程 Oracle 数据库
- javascript - 如何在odoo中覆盖js函数
- c# - 为什么我们使用 ObservableCollection
在 UWP/C# 中? - docker - Gcloud Kubernetes 连接拒绝暴露服务
- python - 根据另一个数据框中的数据为 pandas 列赋值
- javascript - 两个同名函数,我想调用 div 处于活动状态的函数