我想将返回函数的循环数据存储到单独的变量中.这是我的代码请纠正我,如果错了
功能:
def rawi():
a = raw_input("enter value of a")
b = raw_input("enter value of b")
return a, b
#calling the above function:
c = rawi()
for i in c:
print i
Run Code Online (Sandbox Code Playgroud)
我想要的输出应该是这样的:variable1应该具有从variable2返回的值应该具有从b返回的值
谢谢
我有一个csv列表列表,如:
[[0, 0], [0, 2], [1, 1], [2, 0], [0, 3], [1, 2], [2, 1], [3, 0]]
Run Code Online (Sandbox Code Playgroud)
我想要做的是对每个单独的列表求和,即创建一个由每个逗号分隔变量求和组成的新列表,并检查它们是否等于某个值,即:
检查[0 + 0,0 + 2,1 + 1,2 + 0 .....等于某个数字
我已经达到了:
if sum(gcounter)==3:
gamma=True
print(gamma)
else:
pass
Run Code Online (Sandbox Code Playgroud)
我已经尝试了sum(int ...,并尝试使用for循环,但每次尝试不同的方法时它都会抛出相同的错误TypeError:不支持的操作数类型为+:'int'和'list'所以它是sum函数的一个问题
试图解决这个问题坦率地让我感到无精打采,非常感谢任何帮助!
我在这里找到了一个建议,通过自我更新批量类属性.dict .upadte所以我试过了
class test(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def update(self, **kwargs):
self.__dict__.update(kwargs)
d = {'a':1,'b':2,'c':3}
c = test(d)
Run Code Online (Sandbox Code Playgroud)
而且
c = test()
c.update(d)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
TypeError: __init__() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么这不起作用?干杯C.
我知道你应该注意重新定义setattr方法,你可以在循环中结束.
我知道有两个避免循环的解决方案:
1)使用对象超类,调用:
object.__setattr__(instance,attr,value)
Run Code Online (Sandbox Code Playgroud)
2)使用类dict:
self.__dict__[attr] = value
Run Code Online (Sandbox Code Playgroud)
我的问题是,__dict__解决方案是否也不应该在循环中结束?为什么不呢?
是因为那样我们调用的__setattr__是__dict__对象(一切都是对象)还是什么?
如果是这样,它不应该对所有事情都一样吗?
我有一个长字符串,其中包含许多\ n转义序列,这些转义序列应该是换行符。为了正确地编写要读取的字符串,我认为最好根据\ n字符分割字符串,然后将每个字符串分别写入结果列表中,以实现所需的效果。但是,这行不通,只是没有正确地拆分它们。以下是我的代码,为了清楚起见,我尝试将\ n和\ n都作为拆分,因为我试图在字符串中的文字\ n处拆分。谢谢你的帮助。
shellreturn = subprocess.check_output(["C:\Python34\python",root.wgetdir + "\html2text.py", keyworddir + "\\" + item])
print(shellreturn)
shelllist = (str(shellreturn).split("\\n"))
Run Code Online (Sandbox Code Playgroud) 我知道我可以这样做:
z = a if switch == 1 else z = b
Run Code Online (Sandbox Code Playgroud)
但如果我想要z=a if switch=1, z=b if switch=2, and z=c if switch=3怎么办?是否有一种python有效的方法将其写为单行?
就像是:
z = a if switch == 1 else z = b if switch == 2 else z = c
谢谢,现在只是学习Python(显然).
我试图在python上使用字典作为switch case,但是,参数似乎没有传递给被调用的函数,请帮助:
def switchcase(num,cc):
def fa(num):
out= num*1.1;
def fb(num):
out= num*2.2;
def fc(num):
out= num*3.3;
def fd(num):
out= num*4.4;
options = {
"a":fa(num),
"b":fb(num),
"c":fc(num),
"d":fd(num)
}
out=0
options[cc];
return out
print switchcase(10,"a")
Run Code Online (Sandbox Code Playgroud)
输出为0,我无法弄清楚问题
(Python 3.5.2)
我__repr__为我的一个课程定义如下:
class d():
def __init__(self):
self._values = []
return
def __pos__(self):
return self._values[0]
def __repr__(self, value):
self._values.append(value)
return
Run Code Online (Sandbox Code Playgroud)
现在我想要
x = d()
print(x,"a")
print(+x)
Run Code Online (Sandbox Code Playgroud)
回来
a
但我得到了
TypeError: __repr__() missing 1 required positional argument: 'value'
我尝试了一些变化,比如print(x),"a"没有运气.
让我们说我有一个A大小的numpy矩阵Nx2.我正在做的是计算第一列和第二列的四象限反正切,如下所示:
import math
for i in xrange(A.shape[0]):
phase[i] = math.atan2(A[i,0], A[i,1])
Run Code Online (Sandbox Code Playgroud)
但是,我想以矢量化的方式做到这一点.我怎样才能做到这一点?math.atan2()函数似乎不支持向量化.
谢谢!
我有一个列表看起来像[[23,34],[43,23]],我想为每个数字添加一个int,所以说如果我想添加2到它列表变成[[25,36],[45,25]]
所以我理解Python已经扩展了切片,它将使用[start:end:step]获取子串,其中包含start和end的默认值.
例如:
L = range(6)
>>>L[::2]
[0, 2, 4, 6]
Run Code Online (Sandbox Code Playgroud)
好吧有意义......没有启动值,所以它默认为0,然后递增2.
>>>L[::-1]
[6, 4, 2, 0]
Run Code Online (Sandbox Code Playgroud)
现在这是我真的很困惑的地方.没有值,因为它应该是0,但它从列表的后面开始......?不应该是[0,6,4,2]吗?为什么step = -1给出了从列表后面开始的特殊行为?
#C:/Python32
class Person:
def __init__(self, name = "joe" , age= 20 , salary=0):
self.name = name
self.age = age
self.salary = salary
def __printData__(self):
return " My name is {0}, my age is {1} , and my salary is {2}.".format(self.name, self.age, self.salary)
print(Person)
class Employee(Person):
def __init__(self, name, age , salary ):
Person. __init__ (self,name = "Mohamed" , age = 20 , salary = 100000)
def __printData__(self):
return " My name is {0}, my age is {1} , and my …Run Code Online (Sandbox Code Playgroud) >>> "Monty" < "Python"
True
>>> "Z" < "a"
True
>>> "Monty" < "Montague"
False
Run Code Online (Sandbox Code Playgroud)
关于那个规则是什么?是字母的数量还是什么?谢谢
python ×13
list ×2
class ×1
if-statement ×1
inline ×1
loops ×1
math ×1
newline ×1
numpy ×1
python-2.7 ×1
python-3.x ×1
setattr ×1
split ×1
trigonometry ×1