简体   繁体   English

如何分别从字符串中检索最后两条数据?

[英]How to retrieve last two pieces of data from string individually?

This code retrieves the last two odds fluctuations for a particular runner in a race.此代码检索特定赛跑者在比赛中的最后两个赔率波动。 I want to retrieve the last two fluctuations, but I want to retrieve them individually.我想检索最后两个波动,但我想单独检索它们。

flucs2 = flucs1[-2:] retrieves the last two fluctuations, but not individually. flucs2 = flucs1[-2:]检索最后两个波动,但不是单独检索。

flucs2 = flucs1[-1:] retrieves the last fluctuation individually flucs2 = flucs1[-1:]检索最后的波动

What do I use to retrieve the second last fluctuation individually?我用什么来单独检索倒数第二个波动?

My code:我的代码:

request2 = requests.get('https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId=1051322&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'}) 
json2 = request2.json()
data = []
for selection in json2['selections']:
    for fluc in selection['flucs'][0]:
        flucs1 = ast.literal_eval(selection['flucs'])
        flucs2 = flucs1[-2:]

Using negative indexing will count from the back so -1 is the last item, -2 is the second last item etc. You don't need to splice with : to get just one item:使用负索引将从后面开始计数,因此-1是最后一项, -2是倒数第二项等。您不需要与:拼接以获得一个项目:

>>> list = ["one", "two", "three", "four"]
>>> list[-1]
'four'
>>> list[-2]
'three'

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

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