Python 在列表中搜索号码
我有一个列表,数据被分割成字符串,列表如下所示Python 在列表中搜索号码,python,Python,我有一个列表,数据被分割成字符串,列表如下所示 ['Equifax', 'BUY', 'Icelandic', 'Krona:', '41983'] 我想把它分开,这样每个值都有一个不同的变量,所以我使用了下面的代码 yourlist = line.split() company=yourlist[0] action=yourlist[1] 我的问题是,我需要将货币设置为行动后和列表中最终值之前的所有货币,以便冰岛和克朗成为货币。那么,如何将amount设置为列表的最
['Equifax', 'BUY', 'Icelandic', 'Krona:', '41983']
我想把它分开,这样每个值都有一个不同的变量,所以我使用了下面的代码
yourlist = line.split()
company=yourlist[0]
action=yourlist[1]
我的问题是,我需要将货币设置为行动后和列表中最终值之前的所有货币,以便冰岛和克朗成为货币。那么,如何将amount设置为列表的最后一个元素,然后将curreny设置为action和amount之间的所有元素?您需要列表:
使用列表切片-
currency=l[2:-1]
。
l = ['Equifax', 'BUY', 'Icelandic', 'Krona:', '41983']
# l is a list, no need for split()
company = l[0]
action = l[1]
currency = l[2:-1]
# the previous lines sliced the list starting at the 3rd element
# stopping, but not including, at the last item
amount=l[-1]
# counting backwards [-1] indicates last item in a list.
company
Out[22]: 'Equifax'
action
Out[23]: 'BUY'
currency
Out[24]: ['Icelandic', 'Krona:']
amount
Out[25]: '41983'