| | |
List convert to string
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
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
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
So based on the user input, the system should display
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
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
Python Syntax (Toggle Plain Text)
--number 1,2,3,4
The file is a 2 row file which 1st row represented the user's input
Python Syntax (Toggle Plain Text)
1 20 2 56 3 42 4 53 5 90 6 33
Python Syntax (Toggle Plain Text)
1 20 2 56 3 42 4 53
Can anyone gives me a hint? still stuck in this problem

Many thanks before
feli
1
#2 Nov 6th, 2009
Here's some examples of converting between strings, lists, lists of strings, and ints using
list , str and int : python Syntax (Toggle Plain Text)
>>> 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' >>>
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 Nov 6th, 2009
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.
The error that i got is
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
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.
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
TypeError: int() argument must be a string or a number, not 'list'
Does someone have idea? Or instead of list, i should use array?
many thanks again
0
#4 Nov 6th, 2009
•
•
•
•
Python Syntax (Toggle Plain Text)
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
Python Syntax (Toggle Plain Text)
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
Last edited by masterofpuppets; Nov 6th, 2009 at 1:03 pm.
0
#5 Nov 6th, 2009
•
•
•
•
well you could try this:
Python Syntax (Toggle Plain Text)
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
python Syntax (Toggle Plain Text)
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
python Syntax (Toggle Plain Text)
filtered_data = [data for data in user_input if data in int_data]
0
#6 Nov 6th, 2009
•
•
•
•
I think that this may simply be the same thing using different logic:
And further consolidation gives us this list comprehension:python Syntax (Toggle Plain Text)
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
python Syntax (Toggle Plain Text)
filtered_data = [data for data in user_input if data in int_data]
I don't know how to use this thing though: Python Syntax (Toggle Plain Text)
filtered_data = [data for data in user_input if data in int_data]
0
#7 Nov 6th, 2009
This problem really asks for the use of a dictionary object ...
python Syntax (Toggle Plain Text)
# 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 """
May 'the Google' be with you!
![]() |
Similar Threads
- Convert String to Date (PHP)
- convert to string: list with integers and strings (Python)
- DrScheme - Convert a string into a list of words (Computer Science)
- Convert string to to HEX (C)
- How to convert string to const char* in C (C++)
- Convert string "aA" to a BigInteger (Java)
Other Threads in the Python Forum
- Previous Thread: How to permenantly set PYTHONPATH for 2.6 and 3.0?
- Next Thread: Drwaing Circles?
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array basic beginner book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary dynamic examples excel file filename float format ftp function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery newb number numbers numeric output parsing path phonebook plugin port prime program programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion recursive redirect scrolledtext server software ssh stamp statictext statistics string strings table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame wxpython






