当前位置:网站首页>list.replace, str.append

list.replace, str.append

2022-06-25 12:10:00 燕策西

list.replace, str.append

问题描述

append 为何返回空值?replace 操作又为何频频失效?

# 1):
a = np.array([1., 2., 3.])
b = np.array([1., 2., 4.])
c = list(a)

d = c.append(b)
print(d)

# 2):
s = ' 0.00000000 -0.07024802'
s.replace(' ', '#')
print(s)
None
  0.00000000   -0.07024802

原因及解决方法

list.append不返回任何值,不能用变量接收,而str.replace恰好相反,它返回一个原字符串的复制版本(copy),需要用新变量接收.

# 1):
a = np.array([1., 2., 3.])
b = np.array([1., 2., 4.])
c = list(a)

c.append(b)
print(c)

# 2):
s = ' 0.00000000 -0.07024802'
new = s.replace(' ', '#')
print(new)
[1.0, 2.0, 3.0, array([1., 2., 4.])]
##0.00000000###-0.07024802

完结撒花。

原网站

版权声明
本文为[燕策西]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43543177/article/details/110471684