2015-10-21 76 views
2
names = ['apple', 'banana', 'orange'] 
prices1 = ['0.40', '1.20', '0.35'] 
prices2 = ['0.43', '1.21', '0.34'] 

列表如何生成每个名称列表和价格(S)追加到该列表如何生成每个列表项

如。

fruits = [['apple', ['0.40', '0.43']], 
      ['banana', ['1.20', '1.21']], 
      ['orange', ['0.35', '0.34']]] 

这是我一直在试图使用方法:

x = 0 
n = len(names) 
fruits = [[] for name in names] 
for i in prices: 
    for x in range(0, n-1): 
     x += 1 
     fruits[x].append(prices[x]) 

编辑

我希望能够操纵 - 添加/删除从价格 - 如生成的列表

print[apple]

['0.40', '0.43']

apple.append(prices3[x])

['0.40', '0.43', 'x']

感谢这么多的帮助,我还在学习

+2

'fruits'不显示有效的Python结构。 –

+0

(继续从@ IgnacioVazquez-Abrams所说的话):无论如何,都不像书面那样。你错过了几个逗号。 –

+0

你正在覆盖'价格'。 –

回答

1

编辑 - 使用字典:

现在你已经指定您希望如何处理你的数据,我倒是强烈推荐切换到使用dictionary,而不是名单。由于键和值的关联是如何工作的,字典将允许您通过比数字索引更具描述性的值来访问特定项目,就像列表一样。您的新代码将是这个样子:

>>> names = ['apple', 'banana', 'orange'] 
>>> prices1 = ['0.40', '1.20', '0.35'] 
>>> prices2 = ['0.43', '1.21', '0.34'] 
>>> 
>>> fruits = {}  # fruits is now a dictionary, which is indicated by the curly braces 
>>> for i in range(len(names)): 
...  fruits[ names[i] ] = [ prices1[i], prices2[i] ] 
... 
>>> print(fruits) 
{'orange': ['0.35', '0.34'], 'apple': ['0.40', '0.43'], 'banana': ['1.20', '1.21']} 

而且如果你需要检查了在特定水果的价格,你总是可以使用:

>>> print(fruits['apple']) 
['0.40', '0.43'] 

同样,为了增加新的价格,你只需要键入:

>>> fruits['banana'].append('1.80') 
>>> print(fruits['banana']) 
['1.20', '1.21', '1.80'] 

,并消除价格:

>>> fruits['orange'].remove('0.34') 
>>> print(fruits['orange']) 
['0.35'] 

要插入一个全新的项目到字典中,只需使用=运营商将其归因于新的密钥:

>>> fruits['durian'] = ['2.25', '2.33'] 
>>> print(fruits) 
{'orange': ['0.35'], 'durian': ['2.25', '2.33'], 'apple': ['0.40', '0.43'], 'banana': ['1.20', '1.21', '1.80']} 

,并删除一个项目,只需调用pop方法:

>>> fruits.pop('apple') 
['0.40', '0.43'] 
>>> print(fruits) 
{'orange': ['0.35'], 'durian': ['2.25', '2.33'], 'banana': ['1.20', '1.21', '1.80']} 

通过这种方式,您可以更清楚地知道您在任何给定时间所操作的内容,而不是试图围绕难以理解的列表索引。

但是,如果您必须使用列表,请参阅下面的旧回答。


老答案:

假设用于本应该被分配到两个不同的变量价格的两个列表,一个解决办法是遍历列表,像这样:

>>> names = ['apple', 'banana', 'orange'] 
>>> prices1 = ['0.40', '1.20', '0.35'] 
>>> prices2 = ['0.43', '1.21', '0.34'] 
>>> 
>>> fruits = [] 
>>> for i in range(len(names)): 
...  fruits.append([ names[i], [prices1[i], prices2[i]] ]) 
... 
>>> fruits 
[['apple', ['0.40', '0.43']], ['banana', ['1.20', '1.21']], ['orange', ['0.35', '0.34']]] 
+0

非常感谢!很快,我对此仍有点困惑:如果我只使用一组价格,“对于范围内的我(len(names)): ... fruits + = [names [i],[prices1 [我]]]'然后想要将price2列表添加到水果中。我试过:'为我在范围内(len(公司)): fruits.append [prices2]'但这只是将该集合添加到最后。 '['apple',['0.40'],'banana',['1.20'],'orange',['0.35']'0.43','1.21','0.34'] – jfox

+0

赞...为什么这个工作正如我预期的'fruit [1] .append(prices2 [0])''''fruit [2] .append(prices2 [1])'返回这个错误'AttributeError:'str'object has no attribute'append' ' – jfox

+0

所以我想通了,水果[0] =苹果,水果[1] ='0.40'。有没有办法在水果中引用apple []? – jfox

2

您可以使用zip两次:

names = ['apple', 'banana', 'orange'] 
prices1 = ['0.40', '1.20', '0.35'] 
prices2 = ['0.43', '1.21', '0.34'] 
fruits = list(zip(names, zip(prices1, prices2))) 

在python3,zip是一个发电机,因此我们使用fruits = list(...)将发生器变成列表。

+0

这似乎很简单,我想我需要什么,然后如何访问价格? - 添加或清空。如果我打印(水果[1]),我会得到['apple',('0.40','0.43')] – jfox