我希望将此列表转换为int而不是其中的字符串元素。
只是将其转换为整数。
a = [“ a \ n”,“ 222 \ n”,“ bbb \ n”,“ 7777”]
有办法吗?
我试过了
for x in a:
x = int(a)
但这会全部转换。
参考方案
不知道您的期望是什么。但是我认为您在列表中有该字符串,因此可以获取该字符串。请尝试以下操作,并检查是否有帮助。
#this is all string
#a = ["a\n", "222\n", "bbb\n", "7777"]
#try with the below set
a = [10,11,'1','a','b',12,13,'14','c']
intPart = [x for x in a if isinstance(x, int)]
stringPart = [x for x in a if isinstance(x, str)]
print(intPart)
print(stringPart)
#output
#10, 11, 12, 13]
#['1', 'a', 'b', '14', 'c']
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…
TypeError:'str'对象不支持项目分配,带有json文件的python - python以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…