简体   繁体   中英

subtract two elements from the list

I have a list name prefecther I want to subtract two elements from the list I have written three variants but nothing work Variant1:

i=list[0]-list[1]

variant 2:

sub=list[0]
i=sub-list[1]

variant3

sub1=list[0]
sub2=list[1]
i=sub1-sub2

program:

stride=0
sub1=0
sub2=0
for i in range(0,len(prefetch)):
    if prefetch[1]==0:
        prefetch[1]=prefetch[i]
    for j in range(1,len(prefetch)):
        sub1=prefetch[i]
        sub2=prefetch[j]
        if prefetch[i]==prefetch[j]:
            confidence+=1
        else:

            stride=sub1-sub2

            newaddr=prefetch[i+stride]
            confidence=0

It shows TypeError: unsupported operand type(s) for -: 'list' and 'int'

stride=0

sub1=0

sub2=0

Initialise variable confidence

confidence=0

prefetch=[1,3,5,6,7,8,2]

sample list

for i in range(0,len(prefetch)):

if prefetch[1]==0:

    prefetch[1]=prefetch[i]

for j in range(1,len(prefetch)):

    sub1=prefetch[i]

    sub2=prefetch[j]

    if prefetch[i]==prefetch[j]:

        confidence+=1

    else:

        stride=sub1-sub2

here we need to pass the substracted value to the list position

        prefetch[i]=stride

    confidence=0

print(prefetch)

I dont know your requirement and dont know what is prefetch,

But i assume set of number as a prefetch like below,

and i have added one condition check in line number 15.

here is my code,

prefetch = range(100)
stride=0
sub1=0
sub2=0
for i, v in enumerate(prefetch):
    if prefetch[1]==0:
        prefetch[1]=prefetch[i]
    for j in range(1,len(prefetch)):
        sub1=prefetch[i]
        sub2=prefetch[j]
        if prefetch[i]==prefetch[j]:
            confidence+=1
        else:
            stride=sub1-sub2
            if i+stride < len(prefetch):
                newaddr=prefetch[i+stride]
            confidence=0
print('prefetch', prefetch)
print('confidence', confidence)
print('sub1', sub1)
print('sub2', sub2)
print('newaddr', newaddr)

and here is my printed values,

('prefetch', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
('confidence', 1)
('sub1', 99)
('sub2', 99)
('newaddr', 97)

The above given answer will perfectly work fine with there list values. I think there is another list with in a list that is why it is happening. Some sample code.

list1 = [1,2,3,4,5]

list2 = [[6],'7','8','9','10']

a = list1[4]
b = list2[0]

sub = a-b
print(add)

Output

TypeError: unsupported operand type(s) for -: 'int' and '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