Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 30th, 2012, 04:54 AM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Changing file to dictionary:

I have a file I wanna run through in a loop. My file looks something like this:

Hello Hello
A B C D
A B C D
B C D A
B C D A
C D A B
B C D A

Hello Bye
A C D B
C D A B
A C D B
D C A B

Hello GoodBye
B C D A
A B C D
D B C A
A B C D
and I run a loop only through Bye, to blank line it should return: [from Hello Bye to blank line] i am returning one specific box of things but in my code i have an option of returning all... where my dict will include all of them added

{(A, C, D, B): 2, (C, D, A, b):1, (D, C, A, B):1}
I want to do this without using any import

So far my function looks like this:

def file_to_dict (file, input_word) # input_word = Hello or Bye
new_dict = {}
new_list = []

flag = False
for line in file:

if line.strip() == ('Hello ' + input_word):
flag = True
elif if input_word == 'all' and 'Hello' in line: #input_word is another function in which i can use values such as 'Hello' or ' Good bye' or 'Bye' or 'all'
flag = True
elif 'Hello' in line:

flag = False
elif flag == True and line.strip() != "":
new_list.append(line.split())
for next_element in new_list:
next_element = tuple(next_element)
if next_element in new_dict:
new_dict[next_element] += 1
else:
new_dict[next_element] = 1
return new_dict


Can you tell me possibly how i can do this??
Thanks

Reply With Quote
  #2  
Old November 30th, 2012, 12:38 PM
dwblas dwblas is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 291 dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level)dwblas User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 18 h 33 m 54 sec
Reputation Power: 7
There is an error on the following line and an indentation error after the last for() loop. Please test your code before posting.
Code:
elif if input_word == 'all' and 'Hello' in line: #input_word is another function in which i can use values such as 
Code:
for next_element in new_list:
        next_element = tuple(next_element)
        if next_element in new_dict:
            new_dict[next_element] += 1
        else:
            new_dict[next_element] = 1
    return new_dict 

Last edited by dwblas : November 30th, 2012 at 12:48 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Changing file to dictionary:

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap