gpt4 book ai didi

python - 从列表列表中提取

转载 作者:行者123 更新时间:2023-11-28 20:53:58 25 4
gpt4 key购买 nike

如何提取列表列表中的元素并在 python 中创建另一个元素。所以,我想从中得到:

all_list = [['1 2 3 4','2 3 4 5'],['2 4 4 5', '3 4 5 5' ]]

像这样的新列表:

list_of_lists = [[('3','4'),('4','5')], [('4','5'),('5','5')]]

以下是我所做的,但它不起作用。

for i in xrange(len(all_lists)):
newlist=[]
for l in all_lists[i]:
mylist = l.split()
score1 = float(mylist[2])
score2 = mylist[3]
temp_list = (score1, score2)
newlist.append(temp_list)
list_of_lists.append(newlist)

请帮忙。非常感谢。

最佳答案

您可以使用嵌套列表理解。 (假设您想要每个字符串的最后两个“分数”):

[[tuple(l.split()[-2:]) for l in list] for list in all_list]

关于python - 从列表列表中提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3250974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com