List convert to string

Thread Solved

Join Date: Sep 2009
Posts: 5
Reputation: Felicidas is an unknown quantity at this point 
Solved Threads: 0
Felicidas Felicidas is offline Offline
Newbie Poster

List convert to string

 
0
  #1
26 Days Ago
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
  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
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #2
25 Days Ago
Here's some examples of converting between strings, lists, lists of strings, and ints using list , str and int :
  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. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: Felicidas is an unknown quantity at this point 
Solved Threads: 0
Felicidas Felicidas is offline Offline
Newbie Poster
 
0
  #3
25 Days Ago
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.
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 214
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 51
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
25 Days Ago
  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:

  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; 25 Days Ago at 1:03 pm.
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #5
25 Days Ago
Originally Posted by masterofpuppets View Post
well you could try this:

  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:
  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:
  1. filtered_data = [data for data in user_input if data in int_data]
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 214
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 51
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #6
25 Days Ago
Originally Posted by jlm699 View Post
I think that this may simply be the same thing using different logic:
  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:
  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:
  1. filtered_data = [data for data in user_input if data in int_data]
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,022
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #7
25 Days Ago
This problem really asks for the use of a dictionary object ...
  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. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC