944,194 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1694
  • Python RSS
Nov 6th, 2009
0

List convert to string

Expand Post »
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
Python Syntax (Toggle Plain Text)
  1. --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
Python Syntax (Toggle Plain Text)
  1. 1 20
  2. 2 56
  3. 3 42
  4. 4 53
  5. 5 90
  6. 6 33
So based on the user input, the system should display
Python Syntax (Toggle Plain Text)
  1. 1 20
  2. 2 56
  3. 3 42
  4. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Felicidas is offline Offline
5 posts
since Sep 2009
Nov 6th, 2009
1
Re: List convert to string
Here's some examples of converting between strings, lists, lists of strings, and ints using list , str and int :
python Syntax (Toggle Plain Text)
  1. >>> usr_inp = '123456'
  2. >>> usr_inp2 = '1,2,3,4,5,6'
  3. >>> list(usr_inp)
  4. ['1', '2', '3', '4', '5', '6']
  5. >>> usr_inp2.split(',')
  6. ['1', '2', '3', '4', '5', '6']
  7. >>> list(usr_inp)[4]
  8. '5'
  9. >>> int(list(usr_inp)[4])
  10. 5
  11. >>> str(int(list(usr_inp)[4]))
  12. '5'
  13. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 6th, 2009
0
Re: List convert to string
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.
Python Syntax (Toggle Plain Text)
  1. user_input = 1 2 3 # as list
  2. int_data = 1 2 3 4 5 # as int
  3. # How to loop output only the int_data that has the same value with user_input
  4. # something like :
  5. for user_input in int_data:
  6. print int_data #The output should be : 1 2 3
The error that i got is
Python Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Felicidas is offline Offline
5 posts
since Sep 2009
Nov 6th, 2009
0
Re: List convert to string
Quote ...
Python Syntax (Toggle Plain Text)
  1. user_input = 1 2 3 # as list
  2. int_data = 1 2 3 4 5 # as int
  3. # How to loop output only the int_data that has the same value with user_input
  4. # something like :
  5. for user_input in int_data:
  6. print int_data #The output should be : 1 2 3
well you could try this:

Python Syntax (Toggle Plain Text)
  1. user_input = [ 1, 2, 3 ] #all are integers
  2. int_data = [ 1, 2, 3, 4, 5 ] #all are integers
  3. for num in user_input:
  4. for data in int_data:
  5. if num == data:
  6. 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.
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 6th, 2009
0
Re: List convert to string
well you could try this:

Python Syntax (Toggle Plain Text)
  1. user_input = [ 1, 2, 3 ] #all are integers
  2. int_data = [ 1, 2, 3, 4, 5 ] #all are integers
  3. for num in user_input:
  4. for data in int_data:
  5. if num == data:
  6. 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:
python Syntax (Toggle Plain Text)
  1. user_input = [ 1, 2, 3 ] #all are integers
  2. int_data = [ 1, 2, 3, 4, 5 ] #all are integers
  3. for num in user_input:
  4. if num in int_data:
  5. print num
And further consolidation gives us this list comprehension:
python Syntax (Toggle Plain Text)
  1. filtered_data = [data for data in user_input if data in int_data]
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 6th, 2009
0
Re: List convert to string
Click to Expand / Collapse  Quote originally posted by jlm699 ...
I think that this may simply be the same thing using different logic:
python Syntax (Toggle Plain Text)
  1. user_input = [ 1, 2, 3 ] #all are integers
  2. int_data = [ 1, 2, 3, 4, 5 ] #all are integers
  3. for num in user_input:
  4. if num in int_data:
  5. print num
And further consolidation gives us this list comprehension:
python Syntax (Toggle Plain Text)
  1. 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:
Python Syntax (Toggle Plain Text)
  1. filtered_data = [data for data in user_input if data in int_data]
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 6th, 2009
0
Re: List convert to string
This problem really asks for the use of a dictionary object ...
python Syntax (Toggle Plain Text)
  1. # typical test data
  2. data = """\
  3. 1 20
  4. 2 56
  5. 3 42
  6. 4 53
  7. 5 90
  8. 6 33
  9. """
  10.  
  11. fname = 'mytest123.txt'
  12. # write test data to a file
  13. fout = open(fname, "w")
  14. fout.write(data)
  15. fout.close()
  16.  
  17. # read the test file and convert to user_val:value dictionary
  18. user_dict = {}
  19. for line in open(fname):
  20. # remove trailing newline char
  21. line = line.rstrip()
  22. key, val = line.split()
  23. user_dict[key] = val
  24.  
  25. print user_dict # test
  26. """result -->
  27. {'1': '20', '3': '42', '2': '56', '5': '90', '4': '53', '6': '33'}
  28. """
  29.  
  30. # assume user input from the commandline is ...
  31. user_input = '1,2,3'
  32. for user_val in user_input.split(','):
  33. print user_val, user_dict[user_val]
  34.  
  35. """result -->
  36. 1 20
  37. 2 56
  38. 3 42
  39. """
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to permenantly set PYTHONPATH for 2.6 and 3.0?
Next Thread in Python Forum Timeline: Drwaing Circles?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC