仅从列表中返回所有元组

时间:2015-05-25 07:50:28

标签: python python-2.7

我想只返回列表中的tuple1,tuple2,tuple3

def list_read():
type12=[(u'War', u'Anime', u'Anime', u'Comedy'), (u'DVD', u'DVD', u'DVD', u'VHS'), (u'PG', u'R', u'PG', u'PG')]#...so on]
for each in type12:
    print(each)#my output is (u'War', u'Anime', u'Anime', u'Comedy')
               #(u'DVD', u'DVD', u'DVD', u'VHS')
               # (u'PG', u'R', u'PG', u'PG')
               #...........
return  (u'War', u'Anime', u'Anime', u'Comedy'),(u'DVD', u'DVD', u'DVD', u'VHS'),(u'PG', u'R', u'PG', u'PG')
#these format so that i will save to database
# ie Inserting_Data_base=INSERT INTO table_name(A,B,....) values (%s,%s,%s......)

2 个答案:

答案 0 :(得分:0)

for each in type[:3]:

只会通过前3个元素进行迭代

正如@OBP所提到的 - 你可以

return list[:3]

如果您不需要在for循环

中修改它们,则返回前3个元素

答案 1 :(得分:0)

如果你想返回前3个值:

return lst[:3]

顺便说一下,使用保留字'type'作为变量名是错误的。

相关问题