[英]can anyone please let me know what does this line do in my python CODE:
ok so here is the code and i need to know what does this code do when below condition do not match and is there any simpler way to do that,so i can understand it.好的,这是代码,我需要知道当以下条件不匹配时这段代码会做什么,有没有更简单的方法可以做到这一点,所以我可以理解。
major = []
current_major = []
specs={'row': 2, 'exchange': 'NSE', 'name': 'WIPRO', 'token': 969473}
received_token = [969473,415745,12145]
if specs['token'] not in received_token:
values = major[[x[0] for x in major].index(name[:-3])]
current_major.append(values)
major = current_major
current_major = []
#sht2.range('A2').value = major # using xlwings for live data to excel
major.append(values)
After your reply in comments, here is what values = major[[x[0] for x in major].index(name[:-3])]
is doing.在您回复评论后,这就是
values = major[[x[0] for x in major].index(name[:-3])]
正在做的事情。
# Case 1
major = [['WI',
'WR','WW','WK','WI','WL']]
specs={'row': 2, 'exchange': 'NSE', 'name': 'WIPRO', 'token': 969473}
name = specs['name']
values = major[[x[0] for x in major].index(name[:-3])]
print(values)
# Case 2
major = [['WP',
'WR','WW','WK','WI','WL']]
values = major[[x[0] for x in major].index(name[:-3])]
print(values)
In case 1: You have name[:-3]
which is WI
in major
list hence you get the list major
back at that 0
index as output.情况 1:您的
name[:-3]
是major
列表中的WI
,因此您在该0
索引处获得major
列表,即 output。 ( .index()
function look for the index of the element in major) (
.index()
function 查找主要元素的索引)
Output: Output:
['WI', 'WR', 'WW', 'WK', 'WI', 'WL']
In case 2: WI
is not present in the major
hence you get an error:情况 2:
WI
不存在于major
中,因此您会收到错误消息:
ValueError: 'WI' is not in list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.