Python元组在列表内



我有一个数据结构,如下所示,我如何根据名称追加新值。比如,如果input name是Sam, amount是200。如果Sam已经存在,它应该添加到Sam的列表中,否则它应该在列表中创建一个新元素。

data = [('Sam',[100,120,110]),('Andrew',[80,90,100]),('Gram',[400,300,350]),
('Jeremy',[125, 100]), ('Mike',[75,30])]

遍历元组列表并替换/添加元组

作为一条基本规则,元组是不可变的。所以你不能改变元组。但是,可以用列表中的另一个元组替换一个元组。这就是我们将如何解决你的问题。

代码如下:

data = [('Sam',[100,120,110]),('Andrew',[80,90,100]),('Gram',[400,300,350]),
('Jeremy',[125, 100]), ('Mike',[75,30])]
name = input ('Enter name to add to list : ')
while True:
try:
value = int (input ('Enter value to add to list : '))
break
except:
print ('not a number')
for i,(nm,vals) in enumerate(data):
if nm == name:
vals +=[value]
data[i] = (nm,vals)
break
else:
data.append ((name,[value]))
print (data)

输出如下:名称存在于列表中的示例:

Enter name to add to list : Sam
Enter value to add to list : 250
[('Sam', [100, 120, 110, 250]), ('Andrew', [80, 90, 100]), ('Gram', [400, 300, 350]), ('Jeremy', [125, 100]), ('Mike', [75, 30])]

另一个列表中不存在名称的例子:

Enter name to add to list : Joe
Enter value to add to list : 250
[('Sam', [100, 120, 110]), ('Andrew', [80, 90, 100]), ('Gram', [400, 300, 350]), ('Jeremy', [125, 100]), ('Mike', [75, 30]), ('Joe', [250])]

使用Dictionary代替元组

如果您对另一种方法感兴趣,您可以将元组列表转换为字典。然后将数据插入字典,并将其转换回元组列表。

#convert the list of tuples to a dict of lists 
data_dict = {nm:vals for nm,vals in data}
#use setdefault to add the value. append() allows you to append if key exists
data_dict.setdefault(name,[]).append(value)
#convert it back to a list of tuples
data_list = [(nm,vals) for nm,vals in data_dict.items()]
print (data_list)

与上面的结果相同。

从python字典开始将更适合您要做的事情。但我猜你要么有不能使用它的理由,要么你想学习如何遍历列表。

下面是示例代码:
data = [('Sam',[100,120,110]),('Andrew',[80,90,100]),('Gram',[400,300,350]),
('Jeremy',[125, 100]), ('Mike',[75,30])]

## Create a function so you can call this elsewhere
def append_value(name, value):
## Itterate through the list
for each in data:
## If the passed name equals a name in your list do something
if each[0] == name:
## Append the value to the list in the tuple
each[1].append(value)

## Use return to break the for loop
## Return true if a value was added
return True
## If no name in the list equaled the passed name we get here
## Return false so we know that something went wrong
return False
## Print data before addition
## Output: [('Sam', [100, 120, 110]), ('Andrew', [80, 90, 100]), ('Gram', [400, 300, 350]), ('Jeremy', [125, 100]), ('Mike', [75, 30])]
print(data)
## Append new number to "sam" and print the return
## Output: True
print(append_value('Sam', 400))
## Print data after addition
## Output: [('Sam', [100, 120, 110, 400]), ('Andrew', [80, 90, 100]), ('Gram', [400, 300, 350]), ('Jeremy', [125, 100]), ('Mike', [75, 30])]
print(data)

我注释了每一行,这样您就知道它做了什么,并将输出输出给打印。如果不使用字典,就必须手动遍历整个列表。请让我知道这是否有帮助,或者如果你有任何问题!

字典更适合你。

data.setdefault(name, []).append(value)

如果您坚持使用现有的元组列表,则迭代所有项并读取元组的零索引处的名称并进行相应的操作。又丑又低效。

for items in data:
if item[0] == name:
name_found = True
item[1].append(value)
if not name_found:
data.append((name,[value]))
name_found = False

而简单的迭代可以解决这个问题。但如果你很好奇,速度是至关重要的,而且不能使用dict,那么你可以看看dicthash map是如何工作的。您可以实现相同的

这里我给出了一个例子,但它本身并不平衡,这意味着如果数据结构是满的,您需要将列表index扩展为假设2倍。但它代表了重点思想。

class CC:
index = [None for i in range(100)]

def add_element(self, key, val):
hash_ = abs(hash(key))
i_ = hash_ %100
# it does not check if the index is full, in that case it will
# be an infinite loop
while(self.index[i_] !=None):
if(self.index[i_][0] == key):
self.index[i_][1].append(val)
return
i_ += 1

self.index[i_] = [key, [val]]

def get_cc(self):
return [i for i in self.index if i is not None]
c = CC()
print(c.get_cc())
>>> []
c.add_element('Sam', 100)
c.add_element('Sam', 120)
c.add_element('Sam', 110)
c.add_element('Andrew', 80)
print(c.get_cc())
>>> [['Andrew', [80]], ['Sam', [100, 120, 110]]]

最新更新