小编Enc*_*iby的帖子

TypeError:'dict'对象不可调用

我正在尝试循环输入字符串的元素,并从字典中获取它们.我究竟做错了什么?

number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map(int(x)) for x in input_str.split()]
Run Code Online (Sandbox Code Playgroud)
strikes  = [number_map(int(x)) for x in input_str.split()]
TypeError: 'dict' object is not callable
Run Code Online (Sandbox Code Playgroud)

python dictionary

39
推荐指数
6
解决办法
23万
查看次数

从对象元素返回列表?

我有一个已定义的对象(简化如下),并希望将一个元素作为列表返回.

有没有比下面更简单的方法?

class objectRecord(object):
    def __init__(self,fields):
        self.time=fields[0]
        self.definition=fields[1]
        self.name=fields[2]
        self.source=fields[3]
        self.size=fields[4]
        self.value=fields[5]
        self.increasedsize=fields[6]
        self.count=fields[7]
Run Code Online (Sandbox Code Playgroud)

其余的__init__遗漏

    def getList(self):
        return [self.name,self.definition,self.name,self.source,self.size,self.value,self.increasedsize,self.count]
Run Code Online (Sandbox Code Playgroud)

其余的课程省略

python object

1
推荐指数
2
解决办法
64
查看次数

标签 统计

python ×2

dictionary ×1

object ×1