[英]Converting a list of strings into integers
First, I have searched and found some other solutions that seem very plausible and should work, but for whatever reason after trying them with my data - those solutions don't work. 首先,我搜索并找到了其他看起来很合理并且应该可以使用的解决方案,但是在尝试使用我的数据后由于任何原因,这些解决方案都行不通。 I am using python 2.7.6
我正在使用python 2.7.6
I am attempting to convert a list of strings into integers so I can add them. 我正在尝试将字符串列表转换为整数,以便可以添加它们。
#!/usr/bin/env python
import re
numlist = list()
total = 0
#open the file
f = open('c:/Users/Home/Documents/homework/python_webdata/regex_sum_21441.txt', 'r')
for line in f:
line = line.rstrip()
z= re.findall('[0-9]+', line)
if z != []:
numlist.append(z)
print numlist
#attempting to add up the int's from the list (which doesnt work)
results = [int(i) for i in numlist]
total = sum(results)
My data prints out correctly, and looks something like this before I attempt to convert it to ints 我的数据正确打印,在尝试将其转换为int之前看起来像这样
[['2261'], ['2504', '4529'], ['3698', '2693', '3291'], ['3556', '9753', '4059'], ['113', '8261', '157'], ['8059', '1055'], ['1060'], ['2412', '1860'], ['3589'], ['3319'], ['475'], ['6802',
but I get the following error: 但我收到以下错误:
Traceback (most recent call last): File "C:\\Users\\Home\\Documents\\homework\\python_webdata\\lesson 2 code.py", line 20, in results = [int(i) for i in numlist] TypeError: int() argument must be a string or a number, not 'list'
追溯(最近一次通话最近):文件“ C:\\ Users \\ Home \\ Documents \\ homework \\ python_webdata \\ lesson 2 code.py”,第20行,结果= [int(i)for numlist中的i] TypeError:int( )参数必须是字符串或数字,而不是'list'
As one of the comments put appropriately, you have list of lists containing integers because of the "append" you use. 适当地添加注释之一,由于使用了“ append”,因此您具有包含整数的列表的列表。
Put 放
numlist.extend(z)
instead of 代替
numlist.append(z)
Obviously, the issue here is with the z= re.findall('[0-9]+', line)
as this will return a list of strings, now, after numlist.append(z)
, you are appending the whole z
list into numlist
and that's why you get a list of list of strings. 显然,这里的问题是
z= re.findall('[0-9]+', line)
因为这将返回一个字符串列表,现在,在numlist.append(z)
,您要追加整个z
列表到numlist
,这就是为什么要获取字符串列表的原因。
Now, 现在,
results = [int(i) for i in numlist]
will not work as you have in your numlist
lists of strings and int(i)
cannot convert a list into integers, you still have to flatten it, so, I think your best option is numlist.extend(z)
to keep numlist
flat with strings only (not a list of lists). results = [int(i) for i in numlist]
不能像您在numlist
字符串列表中那样工作,并且int(i)
无法将列表转换为整数,您仍然必须将其展平,所以,我认为您最好的选择是numlist.extend(z)
以仅使numlist
与字符串保持平坦(而不是列表的列表)。
Another solution is to flatten your numlist
this way: 另一个解决方案是通过这种方式展平您的
numlist
:
results = [int(item) for lst in numlist for item in lst]
Note: As improvement to your code and sticking with best practices, it is better to use with open(file, mode) as f:
as this will make sure that your file is closed without mentioning f.close()
explicitly, this way: 注意:为了改进您的代码并坚持最佳实践,最好将
with open(file, mode) as f:
这样可以确保关闭文件时无需显式提及f.close()
,方法如下:
with open('c:/Users/Home/Documents/homework/python_webdata/regex_sum_21441.txt', 'r') as f:
for line in f:
line = line.rstrip()
z= re.findall('[0-9]+', line)
if z != []:
numlist.extend(z)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.