在Python中,我如何解析数字字符串,如"545.2222"相应的浮点值,545.2222?或者将字符串解析为"31"整数,31?
我只是想知道如何将一个浮点数 解析str为a float,并且(单独)将一个int 解析str为一个int.
我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?
def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)
Run Code Online (Sandbox Code Playgroud) 我会提取字符串中包含的所有数字.哪个更适合目的,正则表达式或isdigit()方法?
例:
line = "hello 12 hi 89"
Run Code Online (Sandbox Code Playgroud)
结果:
[12, 89]
Run Code Online (Sandbox Code Playgroud) 我有一些python代码分裂逗号,但不剥离空格:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots ', ' of ', ' spaces', ' here ']
Run Code Online (Sandbox Code Playgroud)
我宁愿最终删除这样的空格:
['blah', 'lots', 'of', 'spaces', 'here']
Run Code Online (Sandbox Code Playgroud)
我知道我可以遍历列表和strip()每个项目,但是,因为这是Python,我猜测有更快,更简单,更优雅的方式.
我有一个字符串"42 0"(例如),需要获取两个整数的数组.我可以.split在空间上做吗?
我正在读取一个整数字符串,"3 ,2 ,6 "并希望它们在列表中[3,2,6]作为整数.这很容易被破解,但是什么是"pythonic"方式呢?
我有一个看起来像这样的非标准CSV文件:
x,y
1,"(5, 27, 4)"
2,"(3, 1, 6, 2)"
3,"(4, 5)"
Run Code Online (Sandbox Code Playgroud)
使用pd.read_csv()导致的结果并不是那么有用,因为元组没有被解析。有迹象表明,解决这个问题(一个现有的答案1,2),但由于这些元组具有异质性的长度,这些答案是不是我有问题是完全有益的。
我想做的是绘图x与y使用熊猫绘图例程。天真的方法会导致错误,因为元组存储为字符串:
>>> # df = pd.read_csv('data.csv')
>>> df = pd.DataFrame({'x': [1, 2, 3],
'y': ["(5, 27, 4)","(3, 1, 6, 2)","(4, 5)"]})
>>> df.plot.scatter('x', 'y')
[...]
ValueError: scatter requires y column to be numeric
Run Code Online (Sandbox Code Playgroud)
我希望得到的结果是这样的:
import numpy as np
import matplotlib.pyplot as plt
for x, y in zip(df['x'], df['y']):
y = eval(y)
plt.scatter(x * np.ones_like(y), y, color='blue')
Run Code Online (Sandbox Code Playgroud)
通过转换数据框并使用df.plot.scatter() …
Stack Overflow 上有很多关于这个一般主题的问答,但它们要么质量很差(通常是初学者的调试问题暗示的),要么以其他方式错过了目标(通常是不够通用)。至少有两种极其常见的方法会使幼稚的代码出错,初学者从关于循环的规范中获益更多,而不是从将问题作为拼写错误或关于打印所需内容的规范中获益。所以这是我尝试将所有相关信息放在同一个地方。
假设我有一些简单的代码,可以对一个值进行计算x并将其分配给y:
y = x + 1
# Or it could be in a function:
def calc_y(an_x):
return an_x + 1
Run Code Online (Sandbox Code Playgroud)
现在我想重复计算 的许多可能值x。我知道for如果我已经有要使用的值列表(或其他序列),我可以使用循环:
xs = [1, 3, 5]
for x in xs:
y = x + 1
Run Code Online (Sandbox Code Playgroud)
while或者,如果有其他逻辑来计算值序列,我可以使用循环x:
def next_collatz(value):
if value % 2 == 0:
return value // 2
else:
return 3 * value + 1
def collatz_from_19():
x = 19
while x != 1:
x …Run Code Online (Sandbox Code Playgroud) 阅读Python - 将一串数字转换为一个 int 列表我试图将字符串 '011101111111110' 转换为一个 int 数组:[0,1,1,1,0,1,1,1,1,1, 1,1,1,1,0]
这是我的代码:
mystr = '011101111111110'
list(map(int, mystr.split('')))
Run Code Online (Sandbox Code Playgroud)
但返回错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-9b0dca2f0819> in <module>()
1 mystr = '011101111111110'
----> 2 list(map(int, mystr.split('')))
ValueError: empty separator
Run Code Online (Sandbox Code Playgroud)
如何拆分具有空拆分分隔符的字符串并将结果转换为 int 数组?
python ×9
integer ×2
split ×2
string ×2
arrays ×1
iteration ×1
list ×1
matplotlib ×1
numbers ×1
pandas ×1
parsing ×1
python-3.x ×1
regex ×1
strip ×1
whitespace ×1