gpt4 book ai didi

python - 在Python中获取列表的每个子列表的第一项

转载 作者:行者123 更新时间:2023-11-28 22:27:00 24 4
gpt4 key购买 nike

我有以下字符串,它是列表的列表:

"ID":[[310.5,1.1612828],[310.0,15.0],[310.0,2.9755309],[309.5,30.0]]

我想将每个列表的所有第一个值抓取到一个变量中,所以在这种情况下输出应该是

print variable

预期输出:

310.5
310.0
310.0
309.5

最佳答案

id = [[310.5,1.1612828],[310.0,15.0],[310.0,2.9755309],[309.5,30.0]]

# If you just want to output to console:
[print(x[0]) for x in id] #if id a list
# if id is in dict named dict_
[print(x[0] for x in dict_['id']

这是一个更长的版本。

dict_ = {'id': [[310.5,1.1612828],
[310.0,15.0],
[310.0,2.9755309],
[309.5,30.0]]
}



#make a list containing position 0 values of sublists
pos_0 = [x[0] for x in dict_['id']]
print(pos_0)

#output them to a file
for entry in pos_0:
print(entry)

关于python - 在Python中获取列表的每个子列表的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44350548/

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