-1

I would like to retain the unique integer in the second column and print the entry in the previous (first) column. But I am having trouble converting a string to an integer and get the error: TypeError: 'int' object is not iterable

Input File

    A     1     108.80
    A     1       8.33
    B     2      45.10
    B     2       3.96
    B     2       3.94
    A     3       1.96
    A     3       2.94

Output

A
B
A

Script

with open('test.pdb') as infile:
        for line in infile:
                data = int(line.split()[1])
                seen = set()
                for number in range(data):
                        unique = [x for x in number if x not in seen]   
                        seen.update(unique)
                        print(unique)
EA00
  • 633
  • 8
  • 19
  • 3
    What do you imagine `[x for x in number if x not in seen]` will give? Particularly `for x in number`, given that `number` is an `int` since you say `for number in range(data)`. – Alex Hall Jun 29 '17 at 17:37
  • If youa re getting an error, post the full error message including the stack trace – juanpa.arrivillaga Jun 29 '17 at 17:38
  • there is an error in your list comprehension `unique = [x for x in number if x not in seen]` , it is trying to loop over the variable `number` which is not a list in your case – Stack Jun 29 '17 at 17:38
  • Possible duplicate of [Split an integer into digits to compute an ISBN checksum](https://stackoverflow.com/questions/974952/split-an-integer-into-digits-to-compute-an-isbn-checksum) – victor Jun 29 '17 at 17:38

2 Answers2

3

This is what you (probably) want:

seen = set()
with open('test.pdb') as infile:
    for line in infile:
        unique, data = line.split()[0:2]
        if data not in seen:
            print(unique)
            seen.add(data)
Błotosmętek
  • 12,717
  • 19
  • 29
  • 1
    Yours is better! – dawg Jun 29 '17 at 17:43
  • 1
    rather than slicing `line.split()`, it's probably easier to use tuple unpacking (`unique, data, *_ = line.split()`) though that's only valid in Python3.4 (maybe 3.5?) or higher – Adam Smith Jun 29 '17 at 17:50
1

Given that example, here is a list comprehension to process that:

with open(fn) as f:
    seen=set()
    print '\n'.join([c1 for c1,c2 in (line.split()[0:2] for line in f) 
                                   if not (c2 in seen or seen.add(c2))])

Prints:

A
B
A
dawg
  • 98,345
  • 23
  • 131
  • 206