473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting dictionary values from list of strings to list of list.

1
I dispose of a dictionary whose values look like a list of strings (d_pro):
Expand|Select|Wrap|Line Numbers
  1. d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
I'd like to turn it into a dictionary whose values look like a list of lists (expected output in d_pro_list) for further processing:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list= {'1': [['1,2,3'], ['4,5,6']], '2': [['1,2,3'], ['4,5,6']]}
I'm trying the given code:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list={}
  2. for k in d_pro.keys():
  3.     for v in d_pro.values():
  4.         for i in v:
  5.             d_pro_list[k]= [i]
But the output is like:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list= {'1': ['1,2,3,4,5,6'], '2': ['1,2,3,4,5,6']}
Can you please help me finding my mistakes? Thanks.
Jul 16 '14 #1
1 1199
bvdet
2,851 Expert Mod 2GB
I am unsure how you actually want it, so I will show both:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
  3. >>> for key in d_pro:
  4. ...     d_pro[key] = [item.split(",") for item in d_pro[key]]
  5. ...     
  6. >>> d_pro
  7. {'1': [['1', '2', '3'], ['4', '5', '6']], '2': [['1', '2', '3'], ['4', '5', '6']]}
  8. >>> d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
  9. >>> for key in d_pro:
  10. ...     d_pro[key] = [[item] for item in d_pro[key]]
  11. ...     
  12. >>> d_pro
  13. {'1': [['1,2,3'], ['4,5,6']], '2': [['1,2,3'], ['4,5,6']]}
  14. >>> 
Jul 16 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: MackS | last post by:
Dear all, I've got several large sets in my program. After performing several operations on these I wish to present one set to the user sorted according to a certain criterion. Is there any...
0
by: Stephen | last post by:
Hi Everyone I'm going to use my first <asp:CheckBoxList> control to create a list of selections which users can choose from and run a search against. In other words they are going to act as the...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
31
by: metiu uitem | last post by:
Say you have a flat list: How do you efficiently get , , ] I was thinking of something along the lines of: for (key,number) in list: print key, number
2
by: Zlatko Matić | last post by:
Hello. How to reference selected values from a multi-select list box, as a criteria in a query ? Is it possible at all? Regards, Zlatko
5
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence....
5
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that...
5
by: zefciu | last post by:
Hi! I want to embed a function in my python application, that creates a two-dimensional array of integers and passes it as a list (preferably a list of lists, but that is not necessary, as the...
3
by: psbasha | last post by:
Hi, Is it not possible to reference other than Dict,tuples ,strings and list datatypes /variables ,when we call the functions? Say int,float and boolean Thanks PSB
2
by: Loginalyn | last post by:
i am doing a Point of Sales sytem.i am having trouble on how will i add all the integer values entered in a list box by clicking a command button with a corresponding integer value. i want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.