Hi everyone,

hope someone can give me some hints about my problem.
I write a program that read users input through command line(args).
This input i put it as a list. User type number that they want separated with comma

--number 1,2,3,4

The next process is to read the file and and use those numbers above to determine which part of the file will be outputted.
The file is a 2 row file which 1st row represented the user's input

1   20
2   56
3   42
4   53
5   90
6   33

So based on the user input, the system should display

1   20
2   56
3   42
4   53

My problem is because the user input i put as lists, python complains that it should be integer.
Can anyone gives me a hint? still stuck in this problem :(
Many thanks before
feli

Recommended Answers

All 6 Replies

Here's some examples of converting between strings, lists, lists of strings, and ints using list , str and int :

>>> usr_inp = '123456'
>>> usr_inp2 = '1,2,3,4,5,6'
>>> list(usr_inp)
['1', '2', '3', '4', '5', '6']
>>> usr_inp2.split(',')
['1', '2', '3', '4', '5', '6']
>>> list(usr_inp)[4]
'5'
>>> int(list(usr_inp)[4])
5
>>> str(int(list(usr_inp)[4]))
'5'
>>>

Hi..
thanks for the help.
I think my explanation is not clear before.
My problem is actually how to filter the input list against the int data.

user_input = 1 2 3 # as list
int_data = 1 2 3 4 5 # as int
# How to loop output only the  int_data that has the same value with user_input
# something like :
for user_input in int_data:
    print int_data  #The output should be : 1 2 3

The error that i got is

TypeError: int() argument must be a string or a number, not 'list'

because i try to filter int with lists, which is not allowed i guess.
Does someone have idea? Or instead of list, i should use array?
many thanks again

Member Avatar for masterofpuppets
user_input = 1 2 3 # as list
int_data = 1 2 3 4 5 # as int
# How to loop output only the  int_data that has the same value with user_input
# something like :
for user_input in int_data:
    print int_data  #The output should be : 1 2 3

well you could try this:

user_input = [ 1, 2, 3 ] #all are integers
int_data = [ 1, 2, 3, 4, 5 ] #all are integers
for num in user_input:
    for data in int_data:
        if num == data:
            print data

you have to be careful though if you want to use this in files, :)
hope it helps :)

well you could try this:

user_input = [ 1, 2, 3 ] #all are integers
int_data = [ 1, 2, 3, 4, 5 ] #all are integers
for num in user_input:
    for data in int_data:
        if num == data:
            print data

you have to be careful though if you want to use this in files, :)
hope it helps :)

I think that this may simply be the same thing using different logic:

user_input = [ 1, 2, 3 ] #all are integers
int_data = [ 1, 2, 3, 4, 5 ] #all are integers
for num in user_input:
    if num in int_data:
        print num

And further consolidation gives us this list comprehension:

filtered_data = [data for data in user_input if data in int_data]
Member Avatar for masterofpuppets

I think that this may simply be the same thing using different logic:

user_input = [ 1, 2, 3 ] #all are integers
int_data = [ 1, 2, 3, 4, 5 ] #all are integers
for num in user_input:
    if num in int_data:
        print num

And further consolidation gives us this list comprehension:

filtered_data = [data for data in user_input if data in int_data]

yeah bro that's much shorter :) I don't know how to use this thing though:

filtered_data = [data for data in user_input if data in int_data]

This problem really asks for the use of a dictionary object ...

# typical test data
data = """\
1   20
2   56
3   42
4   53
5   90
6   33
"""

fname = 'mytest123.txt'
# write test data to a file
fout = open(fname, "w")
fout.write(data)
fout.close()

# read the test file and convert to user_val:value dictionary
user_dict = {}
for line in open(fname):
    # remove trailing newline char
    line = line.rstrip()
    key, val = line.split()
    user_dict[key] = val

print user_dict  # test
"""result -->
{'1': '20', '3': '42', '2': '56', '5': '90', '4': '53', '6': '33'}
"""

# assume user input from the commandline is ...
user_input = '1,2,3'
for user_val in user_input.split(','):
    print user_val, user_dict[user_val]

"""result -->
1 20
2 56
3 42
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.