简体   繁体   English

如何将元组列表更改为单个列表?

[英]how to change a list of tuples into individual lists?

i have the following lists which consists of tuples, every tuple has items related together the items in every tuple represents an animal, his speed and his weight respictivly我有以下由元组组成的列表,每个元组都有相关的项目每个元组中的项目代表一种动物,他的速度和他的体重分别代表

tuples_list=[(cat,250,50),(lion,650,400),(tiger,700,450)]

firstly i wanted to arrange the tuple_list in a descinding manner according to the second item in the tuples so i used the following首先,我想根据元组中的第二项以决定性的方式排列 tuple_list,所以我使用了以下

def sorted_tuple(tuples_list):
    return(sorted(tuple_lists, key = lambda x: x[1], reverse=True))#to sort the tuples inside the list from highest to lowest speed

so the tuples_list became:所以 tuples_list 变成了:

tuples_list=[(tiger,700,450),(lion,650,400),(cat,250,50)]

now i need to do the following separate the animals into a list, their speed into a second list and their weight into a third list so i did the following现在我需要执行以下操作将动物分成一个列表,将它们的速度放入第二个列表,将它们的重量放入第三个列表,所以我做了以下

animal=[]
speed=[]
weight=[]
animal,speed,weight=zip(*sorted_tuple(tuples_list))

so i expected the following result所以我期待以下结果

animal=[tiger,lion,cat]
speed= [700,  650, 250]
weight=[450,  400,  50]

so every animal has his speed and weight directly below him in the other lists but when i tried to change an item in the speed list into another value under certain condition所以在其他列表中,每只动物的速度和体重都在他的正下方,但是当我试图在特定条件下将速度列表中的一个项目更改为另一个值时

lets say speed[0]=speed[0]+50

i got the following error我收到以下错误

'tuple' object does not support item assignment

so where is the problem exactly please correct me regards那么问题到底出在哪里,请纠正我的问候

OP: where is the problem exactly please correct me OP:问题到底出在哪里,请纠正我

When you tried to convert the list of tuples into list of list using:当您尝试使用以下方法将元组列表转换为列表列表时:

animal,speed,weight=zip(*sorted_tuple(tuples_list))

it did not create list of list rather only tuples ie:它没有创建列表列表,而只是创建元组,即:

speed= (700,  650, 250)

You can still add the speed in the tuple using:您仍然可以使用以下方法在元组中添加速度:

speed[0]+50

But you cannot change their value since tuples in Python are immutable , so you cannot do this:但是你不能改变它们的值,因为tuples in Python are immutable ,所以你不能这样做:

speed[0]=speed[0]+50

Use the index on sorted list of tuples and then assign the animal, speed and weight:使用元组排序列表上的索引,然后分配动物、速度和重量:

def sorted_tuple(tuples_list):
    return(sorted(tuples_list, key = lambda x: x[1], reverse=True))

tuples_list=[('tiger',700,450),('lion',650,400),('cat',250,50)]    
temp_lst = sorted_tuple(tuples_list)

animal = [x[0] for x in temp_lst]
speed =  [x[1] for x in temp_lst]
weight = [x[2] for x in temp_lst]

print(speed[0]+ 50)

EDIT :编辑

You could also achieve the same using zip() with indexing:您也可以使用带有索引的zip()来实现相同的目的:

animal  = list(list(zip(*temp_lst))[0])
speed = list(list(zip(*temp_lst))[1])
weight = list(list(zip(*temp_lst))[2])
print(speed[0]+ 50)

don't use zip function, if preferences are fixed then you can directly iterate within that list, Here it is,不要使用 zip function,如果偏好是固定的,那么你可以直接在该列表中迭代,这里是,

tuples_list=[('tiger',700,450),('lion',650,400),('cat',250,50)]

animal = [i[0] for i in tuples_list]
speed = [i[1] for i in tuples_list]
weight= [i[2] for i in tuples_list]

speed[0]=speed[0]+50

output output

['tiger', 'lion', 'cat']
[750, 650, 250]
[450, 400, 50]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM