472,334 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 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 1034

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 "...
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");...
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...
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...
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...
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...
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...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.