1

I have a list of data of the form:

[line1,a]
[line2,c]
[line3,b]

I want to use a mapping of a=5, c=15, b=10:

[line1,5]
[line2,15]
[line3,10]

I am trying to use this code, which I know is incorrect, can someone guide me on how to best achieve this:

mapping = {"a": 5, "b": 10, "c": 15}
applyMap = [line[1] = 'a' for line in data]

Thanks

EDIT: Just to clarify here, for one line, however I want this mapping to occur to all lines in the file:

Input: ["line1","a"]

Output: ["line1",5]

4
  • 1
    What is the expected output here? Commented Mar 27, 2012 at 8:59
  • Please see my example, perhaps I was unclear, the first form where the array is (string,string) to become the second 'mapped' form of (string,int) Commented Mar 27, 2012 at 9:00
  • Is it a or "a" in your first code sample? Commented Mar 27, 2012 at 9:01
  • Apologies, its ['line1','a'] Commented Mar 27, 2012 at 9:02

4 Answers 4

3

You could try with a list comprehension.

lines = [
   ["line1", "much_more_items1", "a"],
   ["line2", "much_more_items2", "c"],
   ["line3", "much_more_items3", "b"],
]
mapping = {"a": 5, "b": 10, "c": 15}
# here I assume the key you need to remove is at last position of your items
result = [ line[0:-1] + [mapping[line[-1]] for line in lines ]
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do this without specifying the x? My actual data has many more columns - I simplified it to make my explanation more clear.
@user1220022 : I updated my answer for the case you don't have only 2 members in your list items
This is exactly what I was after, I forgot about list iterations. Thanks!
1

Try something like this:

data = [
    ['line1', 'a'],
    ['line2', 'c'],
    ['line3', 'b'],
]

mapping = {"a": 5, "b": 10, "c": 15}

applyMap = [[line[0], mapping[line[1]]] for line in data]

print applyMap

Comments

0
>>> data = [["line1", "a"], ["line2", "b"], ["line3", "c"]]
>>> mapping = { "a": 5, "b": 10, "c": 15}
>>> [[line[0], mapping[line[1]]] for line in data]
[['line1', 5], ['line2', 10], ['line3', 15]]

5 Comments

Similarly, is there a way to do this without specifying the columns number? My actual data has many more columns - I simplified it to make my explanation more clear
@user1220022 Could you post the complete input / output you want in your question?
This output is 100% correct, however in my actual data I have 15 columns. So I don't want to do: [[line[0], mapping[line[1]], line[2],line[3]...,line[14],line[15]] for line in data]
You don't have to do that, as long as data is an array, it'll be completely iterated over.
So I only need to handle the columns before the mapping? ie your example code and the other columns will fill in on their own?
0
lineMap = {'line1': 'a', 'line2': 'b', 'line3': 'c'}  
cha2num = {'a': 5, 'b': 10, 'c': 15}  
result = [[key,cha2num[lineMap[key]]] for key in lineMap]  
print result  

what you need is a map to relevance 'a' -> 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.