这篇文章主要围绕AttributeError:'str'对象没有属性'append'和哪个对象没有caption属性展开,旨在为您提供一份详细的参考资料。我们将全面介绍AttributeError:'
这篇文章主要围绕AttributeError:'str'对象没有属性'append'和哪个对象没有caption属性展开,旨在为您提供一份详细的参考资料。我们将全面介绍AttributeError:'str'对象没有属性'append'的优缺点,解答哪个对象没有caption属性的相关问题,同时也会为您带来AttributeError: 'ADC' 对象没有属性 'atten'、AttributeError: 'str' 对象没有属性 'dot'、AttributeError: 'str' 对象没有属性 'reshape'、AttributeError:'list'对象没有属性'strip'的实用方法。
本文目录一览:- AttributeError:'str'对象没有属性'append'(哪个对象没有caption属性)
- AttributeError: 'ADC' 对象没有属性 'atten'
- AttributeError: 'str' 对象没有属性 'dot'
- AttributeError: 'str' 对象没有属性 'reshape'
- AttributeError:'list'对象没有属性'strip'
AttributeError:'str'对象没有属性'append'(哪个对象没有caption属性)
如何解决AttributeError:''str''对象没有属性''append''?
myList [1]是myList的元素,其类型是字符串。
myList [1]是str,您不能附加它。myList是一个列表,您应该已经附加了它。
>>> myList = [1, ''from form'', [1,2]]
>>> myList[1]
''from form''
>>> myList[2]
[1, 2]
>>> myList[2].append(''t'')
>>> myList
[1, ''from form'', [1, 2, ''t'']]
>>> myList[1].append(''t'')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ''str'' object has no attribute ''append''
>>>
解决方法
>>> myList[1]
''from form''
>>> myList[1].append(s)
Traceback (most recent call last):
File "<pyshell#144>",line 1,in <module>
myList[1].append(s)
AttributeError: ''str'' object has no attribute ''append''
>>>
为什么myList[1]
被认为是''str''
物体?mList[1]
返回列表中的第一项,''from
form''
但我不能追加到列表中的第一项myList
。谢谢。
Edit01:
@pyfunc:谢谢您的解释;现在我明白了。
我需要一个清单清单。因此“来自表单”应为列表。我做到了(如果这不是正确的方法,请更正):
>>> myList
[1,''from form'',[1,2,''t'']]
>>> s = myList[1]
>>> s
''from form''
>>> s = [myList[1]]
>>> s
[''from form'']
>>> myList[1] = s
>>> myList
[1,[''from form''],''t'']]
>>>
AttributeError: 'ADC' 对象没有属性 'atten'
如何解决AttributeError: ''ADC'' 对象没有属性 ''atten''?
我的 pyboard 有点问题。我想测量电压,我想使用这个程序:
from machine import Pin,ADC
adc=ADC(Pin(''A0''))
adc.atten(ADC.ATTN_11DB)
3.3/4095
adc.read()* 0.000805
但我有属性 atten
的问题并阅读
解决方法
问题是因为 ADC 没有名为 ADC.atten() 的方法可以调用。您可以看到 here 的所有方法。
AttributeError: 'str' 对象没有属性 'dot'
如何解决AttributeError: ''str'' 对象没有属性 ''dot''?
我正在使用 qndiag 库来尝试找到 2 个给定矩阵的对角线。
github 在这里:qndiag libray
我正在使用这个 Python 脚本来计算这 2 个尽可能封闭的对角线:
import os,sys
import numpy as np
from qndiag import qndiag
# dimension
m=7
# number of matrices
n=2
# Load spectro and WL+GCph+XC
FISH_GCsp = np.loadtxt(''Fisher_GCsp_flat.txt'')
FISH_XC = np.loadtxt(''Fisher_XC_GCph_WL_flat.txt'')
# Marginalizing over uncommon parameters between the two matrices
COV_GCsp_first = np.linalg.inv(FISH_GCsp)
COV_XC_first = np.linalg.inv(FISH_XC)
COV_GCsp = COV_GCsp_first[0:m,0:m]
COV_XC = COV_XC_first[0:m,0:m]
# Invert to get Fisher matrix
FISH_sp = np.linalg.inv(COV_GCsp);
FISH_xc = np.linalg.inv(COV_XC);
# Drawing a random set of commuting matrices
C=np.zeros((n,m,m));
C[0,:,:] = FISH_sp
C[1,:] = FISH_xc
[D,B] = qndiag(C,''max_iter'',1000,''tol'',1e-3);
# Print expected diagonal matrices
B*C[0,:]*B.T
B*C[1,:]*B.T
给定我的 2 个矩阵 7x7 FISH_sp
和 FISH_xc
,我得到了一个错误:
approximate_joint_diagonalization_qndiag/qndiag/qndiag/qndiag.py",line 90,in qndiag
D = transform_set(B,C)
和关注
approximate_joint_diagonalization_qndiag/qndiag/qndiag/qndiag.py",line 151,in transform_set
op[i] = M.dot(d.dot(M.T))
AttributeError: ''str'' object has no attribute ''dot''
这里是相关的function transform_set
:
def transform_set(M,D,diag_only=False):
n,p,_ = D.shape
if not diag_only:
op = np.zeros((n,p))
for i,d in enumerate(D):
op[i] = M.dot(d.dot(M.T))
以及在我的初始化中调用的主函数:
def qndiag(C,B0=None,weights=None,max_iter=1000,tol=1e-6,lambda_min=1e-4,max_ls_tries=10,diag_only=False,return_B_list=False,verbose=False):
"""Joint diagonalization of matrices using the quasi-Newton method
Parameters
----------
C : array-like,shape (n_samples,n_features,n_features)
Set of matrices to be jointly diagonalized. C[0] is the first matrix,etc...
B0 : None | array-like,shape (n_features,n_features)
Initial point for the algorithm. If None,a whitener is used.
weights : None | array-like,)
Weights for each matrix in the loss:
L = sum(weights * KL(C,C'')) / sum(weights).
No weighting (weights = 1) by default.
max_iter : int,optional
Maximum number of iterations to perform.
tol : float,optional
A positive scalar giving the tolerance at which the
algorithm is considered to have converged. The algorithm stops when
|gradient| < tol.
lambda_min : float,optional
A positive regularization scalar. Each eigenvalue of the Hessian
approximation below lambda_min is set to lambda_min.
max_ls_tries : int,optional
Maximum number of line-search tries to perform.
diag_only : bool,optional
If true,the line search is done by computing only the diagonals of the
dataset. The dataset is then computed after the line search.
Taking diag_only = True might be faster than diag_only=False
when the matrices are large (n_features > 200)
return_B_list : bool,optional
Chooses whether or not to return the list of iterates.
verbose : bool,optional
Prints informations about the state of the algorithm if True.
Returns
-------
D : array-like,n_features)
Set of matrices jointly diagonalized
B : array,n_features)
Estimated joint diagonalizer matrix.
infos : dict
Dictionnary of monitoring informations,containing the times,gradient norms and objective values.
References
----------
P. Ablin,J.F. Cardoso and A. Gramfort. Beyond Pham''s algorithm
for joint diagonalization. Proc. ESANN 2019.
https://www.elen.ucl.ac.be/Proceedings/esann/esannpdf/es2019-119.pdf
https://hal.archives-ouvertes.fr/hal-01936887v1
https://arxiv.org/abs/1811.11433
"""
t0 = time()
n_samples,_ = C.shape
if B0 is None:
C_mean = np.mean(C,axis=0)
d,p = np.linalg.eigh(C_mean)
B = p.T / np.sqrt(d[:,None])
else:
B = B0
if weights is not None: # normalize
weights_ = weights / np.mean(weights)
else:
weights_ = None
D = transform_set(B,C)
为什么点运算符会出现这个错误?它似乎是矩阵产品(如 np.dot
),但它的使用方式让我们认为并非如此。
解决方法
问题在于 [D,B] = qndiag(C,''max_iter'',1000,''tol'',1e-3)
,B0
(这是第二个参数)被分配为字符串而不是数组!那么最终 B
将是一个字符串,因此错误消息 str 对象没有属性 ''dot'' !,如果您仅将 C
矩阵作为参数传递,只需执行 [D,B] = qndiag(C)
。>
AttributeError: 'str' 对象没有属性 'reshape'
请您在使用它的地方添加代码块。
另外,我相信你在调用这个函数时传递了错误的参数,我错误地认为你传递了一个字符串作为参数
AttributeError:'list'对象没有属性'strip'
如何解决AttributeError:''list''对象没有属性''strip''?
您已将行分为几列:
parts = (line.split('','') for line in f)
然后尝试剥离每个列列表:
column = (part.strip() for part in parts)
那行不通。去除每一列:
column = ([col.strip() for col in part] for part in parts)
但是,您可能想使用该csv
模块来完成从文件到数据行的转换:
with open("/home/mic/tmp/test.txt", ''rb'') as f:
reader = csv.reader(f, skipinitialspace=True)
for object_ in iter_something(reader):
print(object_)
该skipinitialspace
选项确保删除定界符后紧跟的空格。当然,每行末尾的换行符也会被删除。
解决方法
以下代码导致AttributeError:’list’对象没有属性’strip’,我也没有解决方法:
#!/usr/bin/env python
from __future__ import absolute_import,division,print_function
from itertools import groupby
DATA = [["Test","A","B01",828288,1,7,''C'',5],["Test",''T'',6],171878,3,871963,9,''A'',''G'',1932523,10,4],''X'',667214,14,6]]
def iter_something(rows):
key_names = [''type'',''name'',''sub_name'',''pos'',''s_type'',''x_type'']
chr_key_names = [''letter'',''no'']
for keys,group in groupby(rows,lambda row: row[:6]):
result = dict(zip(key_names,keys))
result[''chr''] = [dict(zip(chr_key_names,row[6:])) for row in group]
yield result
def convert(val):
constructors = [int,str]
for c in constructors:
try:
return c(val)
except ValueError:
pass
def main():
with open("/home/mic/tmp/test.txt") as f:
parts = (line.split('','') for line in f)
column = (part.strip() for part in parts)
for object_ in iter_something(column):
print(object_)
if __name__ == ''__main__'':
main()
今天的关于AttributeError:'str'对象没有属性'append'和哪个对象没有caption属性的分享已经结束,谢谢您的关注,如果想了解更多关于AttributeError: 'ADC' 对象没有属性 'atten'、AttributeError: 'str' 对象没有属性 'dot'、AttributeError: 'str' 对象没有属性 'reshape'、AttributeError:'list'对象没有属性'strip'的相关知识,请在本站进行查询。
本文标签: