求和在Python中仅作为一个索引计算的列表

求和在Python中仅作为一个索引计算的列表,python,Python,嗨,我是一个初学者,我正在尝试将一个列表作为一个索引进行求和 下面的代码是为玩家分发卡片的代码 def component(self): for card in range(2): global componentcard componentcard= listcard[card] t = (re.findall('\d+', componentcard )) x=list(t) x = [i.spl

嗨,我是一个初学者,我正在尝试将一个列表作为一个索引进行求和

下面的代码是为玩家分发卡片的代码

def component(self):    
    for card in range(2):
        global componentcard
        componentcard= listcard[card]
        t = (re.findall('\d+', componentcard ))
        x=list(t)
        x = [i.split('\n')[0] for i in x] 
        print x[0],

输出给了我['1']['13'],其中如果我打印索引0,它将给我两个不同的卡片值,但作为相同的索引0。这给了我添加相同索引的问题。你知道吗?非常感谢

您有两个名为
x
的不同列表。一个在循环的第一次迭代中构造,另一个在第二次迭代中构造。由于两个列表具有相同的名称,因此第二个列表将覆盖第一个列表。作为旁注,不需要使用行
global componentcard
x=list(t)
,因为
t
已经是一个列表,而且您不太可能希望
componentcard
成为一个全局变量。非常感谢您的帮助,问题仍然是索引仍然是一个变量