473,387 Members | 1,641 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.

Re: Multimapping and string converting

En Fri, 19 Sep 2008 10:59:26 -0300, Ron Brennan <br*********@gmail.com>
escribió:
Hello,

I have a multimap dictionary with a 1 Key to N values. I want to convert
the N values to a string to be used elsewhere in my program.

So I have dict[(1,[1, 2 ,3 ,4])] which I have sorted

When I do a print ''.join(str(dict.value())) I get [1, 2, 3, 4] as an
output
when I really want 1 2 3 4

Here is my code:

dmapItems = dictionary.items()
dmapItems.sort()

for tcKey, tcValue in dmapItems:
file.write('Key = %s\nValue = %s" % (tcKey, tcValue)

stinger = ''.join(str(tcValue))

print stringer

The Output = [145, 2345, 567, 898]
I need it to be 145 2345 567 898
I guess you probably tried using ' '.join(value) and got an error like
this:
TypeError: sequence item 0: expected string, int found
so you inserted that str(...), but you don't still get what you want.
You want "145 2345 567 898". *If* you had a list like this: value =
["145", "2345", "567", "898"] (that is, a list of strings) *then* '
'.join(value) would do what you want. But you have this to start with
instead: value = [145, 2345, 567, 898] (a list of numbers), how to convert
it into a list of strings?
str(value) returns a single string that "looks like" a list but isn't:
"[145, 2345, 567, 898]". Instead, you need to convert each element
individually, and keep them in a list. Try this:
value = [str(elem) for elem in value]
Also, I'd use sorted() to iterate over all items. PUtting all together:

for tcKey, tcValue in sorted(dictionary.items()):
values = [str(elem) for elem in tcValue]
values = ' '.join(values)
print tcKey, values

or perhaps:

for tcKey, tcValue in sorted(find_a_good_name_for_me.iteritems()):
values_str = ' '.join(str(elem) for elem in tcValue)
print tcKey, values_str

(using lazy objects (iteritems and a generator expression) instead of
concrete lists)

--
Gabriel Genellina

Sep 19 '08 #1
0 1086

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Thomas Philips | last post by:
Consider the following simple dictionary e={1:'one', 2: 'two'} e >>>'one' However, If I attempt to print e using a formatted string print " %(1)s" %e, I get a KeyError: '1'
3
by: Cybertof | last post by:
Hello, I would like to return the good single value from a string value in these cases : Convert.ToSingle("23,30"); Convert.ToSingle("23.30"); // Conversion Error !!! The result should be...
8
by: tim | last post by:
This is probably another newbie question...but... even after reading quite some messages like '..hex to decimal', 'creating a hex value' , I can't figure this out: If i do >>> m=66 >>> n=hex(m)...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
2
by: Deephay | last post by:
Greetings all, I have a problem with encode converting: Say, I have a string here, if I print it to stdout, I will got an ascii encoded string (locale do not effect), but I know that this string...
4
by: Jonathan Wood | last post by:
Is it just me? It seems like one moving from MFC to C# loses some string functionality such as the two mentioned in the subject. Did I miss something? -- Jonathan Wood SoftCircuits...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.