简体   繁体   中英

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

I am writing a function delta that takes two arguments: a partitioning of a phone book and a list or tuple containing the distribution of the first letters of telephone subscriber's last names. The function must return the delta of the given partitioning of the phone book for the given distribution of the first letters of telephone subscriber's last names.

A  B C  D  E  F G H I J  K L  M  N 0 P Q R  S  T U V W X Y Z
16 4 17 10 15 4 4 6 7 14 9 17 27 6 1 9 0 12 20 8 0 3 4 0 3 4

For example: in total there are 220 names and the telephone company has decided to print 4 volumes. Ideally, each of these volumes should thus contain 220 / 4 = 55 names. One possible partitioning is AD EJ KO PZ with counts of 47, 50, 60 and 63 names. In absolute value these counts deviate 8, 5, 5 and 8 names from the ideal 55 names per volume. We refer to the total sum of these deviations as the delta of the partitioning of the phone book. For this partitioning, the delta is thus equal to 26.

I have the following error in my code:

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

Anybody knows what I am doing wrong? The error appears to be in the last lines of the last function delta() . I also tried by making an integer of the elements of the list or a string of the list but it is still not working.

def counts(seq):
    '''
    >>> counts('A-D E-J K-O P-Z')
    (4, 6, 5, 11)
    >>> counts('A-G H-O P Q-Z')
    (7, 8, 1, 10)
    >>> counts('A-D E-K L-P Q-Z')
    (4, 7, 5, 10)
    >>> counts('A-D F-K L-P Q-Z')
    Traceback (most recent call last):
    AssertionError: invalid partitioning
    '''
    r = str(seq)

    input_string = r.split(' ')

    q = []
    for dletter in input_string:
        if '-' in dletter:
            q.append(ord(dletter[2]) - ord(dletter[0]) + 1)
        else:
            q.append(1)


    assert sum([x for x in q]) == 26, "invalid partitioning"

    chars = "abcdefghijklmnopqrstuvwxyz"
    chars = chars.upper()
    for char in chars:
        count = r.count(char)
    assert count == 1, "invalid partitioning"

    return (tuple(q))


def delta(seq, names):
    '''
    >>> names = (16, 4, 17, 10, 15, 4, 4, 6, 7, 14, 9, 17, 27, 6, 1, 9, 0, 12, 20, 8, 0, 3, 4, 0, 3, 4)
    >>> delta('A-D E-J K-O P-Z', names)
    26.0
    >>> delta((7, 8, 1, 10), names)
    94.0
    >>> delta('A-D E-K L-P Q-Z', names)
    18.0
    >>> delta(42, names)
    Traceback (most recent call last):
    AssertionError: invalid partitioning
    '''

    r = counts(seq)

    import itertools
    q = list(itertools.accumulate(r))

    j = list()

    for i in q:
        f = sum(names[:i])
        j.append(f)

    s = [t - s for s, t in zip(j, j[1:])]
    d = [j[:1]]

    k = d + s

    g = list(abs(x - 55) for x in k)

    n = float(sum(g))

    return n

Change d = [j[:1]] to d = j[:1] .

g = list(abs(x - 55) for x in k) variable x is a list because of d = [j[:1]] .

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