使用 Python 将 str 列表为 int
List str to int using Python
我有一个字符串:
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
我想将其转换为以下格式,但为 int;
[((1790.0 , 15563.0) , (1791.0 , 15553.0) , (1790.0 , 15551.0) , (1789.0 , 15549.0) , (1791.0 , 15547.0)) ,((1752.0 , 15451.0) , (1753.0 , 15449.0) , (1762.0 , 15449.0) , (1764.0 , 15451.0) , (1761.0 , 15451.0) , (1758.0 , 15454.0) , (1756.0 , 15453.0))]
到目前为止我尝试了什么?
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
st = ip[15:-2]
#s = st.split(',')
x = st.replace(", ", ") (")
res=[x.replace(" ", " , ")]
list(map(int, res))
这会将上述格式转换为字符串而不是整数。我收到以下错误:ValueError: invalid literal for int() with base 10:
我该如何解决?
试试正则表达式
import re
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
output = [tuple(map(lambda x: int(float(x)),i.split())) for i in re.findall(r'(\d[\d.\s]+\d)', ip)]
print(output)
[(1790, 15563), (1791, 15553), (1790, 15551), (1789, 15549), (1791, 15547), (1752, 15451), (1753, 15449), (1762, 15449), (1764, 15451), (1761, 15451), (1758, 15454), (1756, 15453)]```
这可能对您有帮助:
from re import compile
pattern = compile("(\d+\.\d)+ (\d+\.\d)+")
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
st = ip[15:-2]
print([tuple(float(y) for y in x) for x in pattern.findall(st)])
给出结果:
[(1790.0, 15563.0), (1791.0, 15553.0), (1790.0, 15551.0), (1789.0, 15549.0), (1791.0, 15547.0), (1752.0, 15451.0), (1753.0, 15449.0), (1762.0, 15449.0), (1764.0, 15451.0), (1761.0, 15451.0), (1758.0, 15454.0), (1756.0, 15453.0)]
这不是很漂亮,但它会做你想做的事,而且可能比正则表达式等价物更快:
[tuple(tuple(int(float(n))
for n in pair.split())
for pair in t.split(', '))
for t in ip[16:-3].split(')), ((')]
如果你有Python 3.9+,你可以用更直观的ip.removeprefix('MULTIPOLYGON (((').removesuffix(')))')
替换ip[16:-3]
。
我有一个字符串:
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
我想将其转换为以下格式,但为 int;
[((1790.0 , 15563.0) , (1791.0 , 15553.0) , (1790.0 , 15551.0) , (1789.0 , 15549.0) , (1791.0 , 15547.0)) ,((1752.0 , 15451.0) , (1753.0 , 15449.0) , (1762.0 , 15449.0) , (1764.0 , 15451.0) , (1761.0 , 15451.0) , (1758.0 , 15454.0) , (1756.0 , 15453.0))]
到目前为止我尝试了什么?
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
st = ip[15:-2]
#s = st.split(',')
x = st.replace(", ", ") (")
res=[x.replace(" ", " , ")]
list(map(int, res))
这会将上述格式转换为字符串而不是整数。我收到以下错误:ValueError: invalid literal for int() with base 10:
我该如何解决?
试试正则表达式
import re
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
output = [tuple(map(lambda x: int(float(x)),i.split())) for i in re.findall(r'(\d[\d.\s]+\d)', ip)]
print(output)
[(1790, 15563), (1791, 15553), (1790, 15551), (1789, 15549), (1791, 15547), (1752, 15451), (1753, 15449), (1762, 15449), (1764, 15451), (1761, 15451), (1758, 15454), (1756, 15453)]```
这可能对您有帮助:
from re import compile
pattern = compile("(\d+\.\d)+ (\d+\.\d)+")
ip = 'MULTIPOLYGON (((1790.0 15563.0, 1791.0 15553.0, 1790.0 15551.0, 1789.0 15549.0, 1791.0 15547.0)), ((1752.0 15451.0, 1753.0 15449.0, 1762.0 15449.0, 1764.0 15451.0, 1761.0 15451.0, 1758.0 15454.0, 1756.0 15453.0)))'
st = ip[15:-2]
print([tuple(float(y) for y in x) for x in pattern.findall(st)])
给出结果:
[(1790.0, 15563.0), (1791.0, 15553.0), (1790.0, 15551.0), (1789.0, 15549.0), (1791.0, 15547.0), (1752.0, 15451.0), (1753.0, 15449.0), (1762.0, 15449.0), (1764.0, 15451.0), (1761.0, 15451.0), (1758.0, 15454.0), (1756.0, 15453.0)]
这不是很漂亮,但它会做你想做的事,而且可能比正则表达式等价物更快:
[tuple(tuple(int(float(n))
for n in pair.split())
for pair in t.split(', '))
for t in ip[16:-3].split(')), ((')]
如果你有Python 3.9+,你可以用更直观的ip.removeprefix('MULTIPOLYGON (((').removesuffix(')))')
替换ip[16:-3]
。