简体   繁体   中英

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.

# 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. ( .index() function look for the index of the element in major)
Output:

['WI', 'WR', 'WW', 'WK', 'WI', 'WL']

In case 2: WI is not present in the major hence you get an error:

ValueError: 'WI' is not in list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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