def parse_actor_data(actor_data):
while 1:
line = actor_data.readline().strip()
if line.count('-') > 5:
break
actor_movie = {}
values = []
actor_name = ''
running_list = []
movie = []
for line in actor_data:
position = line.find(')')
running = line[:position + 1]
value = running.split('\t')
for k in value:
if k != '':
running_list.append(k)
actor_name_list = value[0].split(',')
actor_name = actor_name_list[0] + actor_name_list[-1]
for i in range(len(running_list)):
if value[0] == running_list[i]:
position2 = i
movie = running_list[position2+1:]
actor_movie[actor_name] = movie
check = actor_movie.keys()
for c in range(len(check)):
if len(check[c]) < 1:
actor_movie.pop(check[c])
return actor_movie
Problem I'm having now is that only the first item of movie is added into the actor_movie anyone can help? i tried so long for this already i seriously have no idea why isn't this working...
Every time you run:
actor_movie[actor_name] = movie
you're overwriting the last movie that was associated with that actor. Try something like this instead where you're storing a list of movies, not just a single value:
try:
actor_movie[actor_name].append(movie)
except KeyError:
actor_movie[actor_name] = [movie]
There are other ways (defaultdict, dict.setdefault, etc.) to do the same thing but that should get you up and running.
Related
Using variable 'criteria_names' before assignment pylint(E0601).
This error pops up only for the 'criteria_names' list which is already assigned, but not for the other lists, which are assigned the same way. Any pointers or solutions are very appreciated!
CODE SNIPPET:
data = list()
lines = list()
n_vectors = list()
vectors = list()
medium_vectors = list()
classnames = list()
classes_insert_lines = list()
vectors_raw = list()
criteria_names = list()
def DataGet(temp_pulse_file):
data[:] = []
n_vectors[:] = []
vectors[:] = []
medium_vectors[:] = []
classnames[:] = []
classes_insert_lines[:] = []
vectors_raw[:] = []
criteria_names[:] = [] #<- this is where the error pops up
with open(temp_pulse_file, "r", encoding='utf-8') as file:
lines = (file.read()).split("\n")
file.close()
n = -1
for i in range(len(lines)):
data.append(lines[i].split(","))
if data[i][0] == ">":
n += 1
classnames.append(data[i][1])
classes_insert_lines.append(i + 1)
n_vectors.append(0)
vectors_raw.append([])
vectors.append([])
elif data[i][0] == "#":
weights = data[i][1:]
elif data[i][0] == "$":
criteria_names = list(data[i][1:])
else:
n_vectors[n] += 1
vectors[n].append(data[i])
vectors_raw[n].append(lines[i])
n_elems = len(vectors[0][0])
classes = len(classnames)
for i in range(classes):
for j in range(n_vectors[i]):
for k in range(n_elems):
vectors[i][j][k] = float(vectors[i][j][k])
return n_elems, classes, n_vectors, weights, lines, classes_insert_lines, vectors_raw
n_elems, classes, n_vectors, weights, lines, classes_insert_lines, vectors_raw = DataGet(getcurrentdir() + 'db.pulse')
print(' criteria_names:',criteria_names)
When I remove criteria_names[:] = [], no errors whatsoever for the other lists used in the function. Anything helps.
Thanks to #martineau and #Barmar, added global criteria_names at the beginning of the DataGet() function and it worked like a miracle. Weird tho.
def swiss_pairings(self):
players = self.player_standings()
players_paired = []
i = 0
// name = players.keys()[i]
match_player = 1
while match_player < range(len(players)):
// name_match_player = players.keys()[match_player]
player_one_id = Player.objects.get(name=name)
player_two_id = Player.objects.get(
name=name_match_player)
if self.has_played_before(player_one_id, player_two_id):
match_player += 1
else:
self.report_match(player_one_id, player_two_id)
players_paired.append(player_one_id.name, player_two_id.name)
del players[name]
del players[name_match_player]
break
return players_paired
The commented lines are producing that dictionaries are not indexed. What can be the replacement for those two lines?? I want only the "keys".
I am writing binary search tree for book phone. The problem is when I am trying to write the main code. I tried to use dictionary data structure but I cannot figure out how to make contact name as a key. I want to write any name and get phone number as a value.
It gave me an error
Traceback (most recent call last):
File "FastSort.py", line 22, in <module>
print(binarySearch(phonebook, "John"))
File "FastSort.py", line 8, in binarySearch
if alist[midpoint] == item:
KeyError: 1
This is a code
def binarySearch(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
phonebook = {}
phonebook["John"] = 938477566
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781
print(binarySearch(phonebook, "John"))
print(binarySearch(phonebook, "Jack"))
You solution (if alist as really a sorted list) was pretty close so I thought I would help you out a bit:
import random
def binarySearch(alist, item):
first = 0
last = len(alist)-1
while first<=last:
midpoint = (first + last)//2
print midpoint
if alist[midpoint][0] == item:
return alist[midpoint]
else:
if item < alist[midpoint][0]:
last = midpoint-1
else:
first = midpoint+1
return False
items = []
items.append(("John", 938477566))
items.append(("Jack", 938377264))
items.append(("Jill", 947662781))
items = sorted(items)
print(binarySearch(items, "John"))
print(binarySearch(items, "Jack"))
print(binarySearch(items, "Jill"))
In the code below I get the following error:
"local variable 'moodsc' referenced before assignment"
I'm new to programming and python. I'm struggling with interpreting other questions on the similar topic. Any context around this specific code would be helpful.
import re
import json
import sys
def moodScore(sent, myTweets):
scores = {} # initialize an empty dictionary
new_mdsc = {} # intitalize an empty dictionary
txt = {}
for line in sent:
term, score = line.split("\t") # The file is tab-delimited. "\t" means "tab character"
scores[term] = int(score) # Convert the score to an integer.
data = [] # initialize an empty list
for line in myTweets:
tweet = json.loads(line)
if "text" in tweet and "lang" in tweet and tweet["lang"] == "en":
clean = re.compile("\W+")
clean_txt = clean.sub(" ", tweet["text"]).strip()
line = clean_txt.lower().split()
moodsc = 0
pos = 0
neg = 0
count = 1
for word in range(0, len(line)):
if line[word] in scores:
txt[word] = int(scores[line[word]])
else:
txt[word] = int(0)
moodsc += txt[word]
print txt
if any(v > 0 for v in txt.values()):
pos = 1
if any(v < 0 for v in txt.values()):
neg = 1
for word in range(0, len(line)): # score each word in line
if line[word] not in scores:
if str(line[word]) in new_mdsc.keys():
moodsc2 = new_mdsc[str(line[word])][0] + moodsc
pos2 = new_mdsc[str(line[word])][1] + pos
neg2 = new_mdsc[str(line[word])][2] + neg
count2 = new_mdsc[str(line[word])][3] + count
new_mdsc[str(line[word])] = [moodsc2, pos2, neg2, count2]
else:
new_mdsc[str(line[word])] = [moodsc, pos, neg, count]
def new_dict():
for val in new_mdsc.values():
comp = val[0] / val[3]
val.append(comp)
for key, val in new_mdsc.items():
print (key, val[4])
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
moodScore(sent_file, tweet_file)
# new_dict()
if __name__ == '__main__':
main()
Ok #joshp, I think you need to globalise some variables, because the error is 'moodsc referenced before assignment', I think the code only gets as far as moodsc += txt[word] but you may also have trouble with pos and neg.
Try global moodsc and pos etc. before you define moodsc and pos etc. If this doesn't work try global moodsc before moodsc += txt[word] and so forth, you may need to use global in both places for it to work, I often find that this is needed in my code, to globalise it at definition and wherever else you use it (at the start of each function and statement where it is used).
The format that I stored them in was:
website/website/objecthash/xcord/ycord/likelyhood/year/month/datenumber/hour/minutes
Right now I have the bucket I want to pull them out of.
Say I want the most recent 10 stored objects. What is an efficient way to do this?
I have the bucket, what do I do with it?
My solution was something like this for get todays however I'm not sure about the logic for get most recent:
def getKeys():
b = bucket.list()
theKeys=[]
for key in b:
theKeys.append(key)
return theKeys
def getDecompiledToday():
time = datetime.datetime.now()
year =time.strftime("%Y")
month = time.strftime("%m")
day = time.strftime("%D")
keys = getKeys()
objects = []
for k in keys:
splitK= k.split("/")
if splitK[6]==year and splitK[7]==month and splitK[8]==day:
objets.append(bucket.get_key(k))
return
The solution that I came up with.
def getPastAmountDecompiledFromFile(number):
if bucketKeys.__len__() > 0:
Found=[]
latest=bucketKeys[0]
while Found.__len__() < number:
laterFound = False
for k in bucketKeys:
if latest in Found:
latest=k
current = k.split("/")
best = k.split("/")
if k not in Found and latest != k:
if int(current[6]) > int(best[6]):
laterFound=True
if int(current[6]) == int(best[6]) and int(current[7]) > int(best[7]):
laterFound=True
if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) > int(best[8]):
laterFound=True
if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) == int(best[8]) and int(current[9]) > int(best[9]):
laterFound=True
if laterFound:
latest = k
if laterFound:
Found.append(latest)
return getKeyFromKeyNames(Found)
else:
getKeysInFile()
getPastAmountDecompiledFromFile(number)
return