Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to give the user input pattern as input1: 00,01,10,11 and another input as input2: 0.1,0.2,0.24,0.5 and these inputs I am giving alternate value as one by one from these two inputs. For giving user input I am using: input = int(raw_input()). But my desired output should be in separate 2-d array as [[0,0],[0,1],[1,0],[1,1]] and [[0.1],[0.2],[0.24],[0.5]] please give me good idea for this.

share|improve this question
 
Your rules for parsing user input are not clear. Should 0.11 become [0.1, 1] or [0.11], for example? Should 111 be [1, 1, 1], [11, 1], or [1, 11]? Should the values in the inner lists be integers, strings, floats...? Look into str.split. –  jonrsharpe Mar 4 at 14:12
 
If the input is 000,010,101,111, what's the output? I can't follow you clearly. –  zybjtu Mar 4 at 14:15
 
@jonrsharpe actually the input1 is in binary and input2 is in float.And output pattern is as given above –  Latik Mar 4 at 14:17
 
@Latik please update your question with clearer descriptions of: 1. the input data you are expecting; 2. your rules for interpreting it; and 3. what you have written so far. –  jonrsharpe Mar 4 at 14:19
 
@zybjtu the output should be in 2-d pattern as [[0,0,0],[0,1,0],[1,0,1],[1,1,1]], actually these values are in binary. –  Latik Mar 4 at 14:23
add comment

1 Answer

up vote 0 down vote accepted

You can use str.split to convert the string input into a list of substrings:

"0.1,0.2".split(",") == ["0.1", "0.2"]

and map to apply some processing rule to those substrings; in your second case, this is conversion to float:

map(float, "0.1,0.2".split(",")) == [0.1, 0.2]

Your first example (binary strings to lists of integers) will require a custom function for map, for example:

map(lambda s: map(int, s), "001,010".split(",")) == [[0, 0, 1], [0, 1, 0]]

Note that in Python 3.x map is an iterator, so you may need to explicitly convert it to a list.

share|improve this answer
 
here we are giving the value directly but what I want is the value should be given as user input i.e. input = int(raw_input()) –  Latik Mar 4 at 14:36
 
Then either assign the user input to a name then process it: ui = raw_input(prompt); input = map(float, ui.split(",")) or put raw_input into the appropriate place, e.g. input = map(float, raw_input(prompt).split(",")) –  jonrsharpe Mar 4 at 14:39
 
here I am getting error as NameError: name 'prompt' is not defined –  Latik Mar 4 at 14:49
 
Yes, you will need to define prompt, i.e. the message that the user should see requesting their input. For example, prompt = "Enter binary strings, separated by commas: " –  jonrsharpe Mar 4 at 14:55
 
ok.. I got this but still the output is not in 2-d array, it is giving as [0.1,0.21,0.114] and what i want is [[0.1],[0.21],[0.114]] –  Latik Mar 4 at 15:01
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.