My code is as follows:
self.newMonsterList =[]
myKeys = []
if (len(self.myList)>0):
for i in range(len(self.myList)-1):
myVar = self.myList[i]
myKeys.append(myVar[1])
lis = game.active_entities.keys()
for k in lis:
if (k not in myKeys):
game.active_entities[k].destroy()
for it in self.myList:
if (it[1] in lis):
self.setEnt(it, game.active_entities[it[1]])
else:
self.newMonsterList.append(it)
self.makeEnt()
This code, that is the method in which it is, is called in a while loop which apparently works.
makeEnt appends an Entity with it[1] as Key to self.newMonsterList.
This works, I have tested it. Therefore, an ID which is in newMonsterList after the first full round of all this code (i.e. of the while loop which calls this code) should not be there after the second.
It is, however.
If, on the other hand, I check in makeEnt whether or not the content of newMonsterList already is a key, I get correct results.
I therefore conclude that newMonsterList is not properly emptied in which run of the while-loop.
I have been trying different versions for 2 hors now....anyone got an idea?
I am trying to write that down simpler:
a is a list of Ints
d is a dict with ints as keys
While True:
b=[]
for it in a:
if (it not in d):
b.append(it)
print it
else:
print it
for k in b:
d[k] = k
I found out now that the monsterlist is indeed emptied at each turn, however, in the ifclause ("if (it not in d):"), "it" is appened to the list each turn. However, "it" is also definitely in d after the first iteration, I can print it.
You are aware that this loop iterates over self.myList, but skips the last item in the list, right?
myKeys = []
if (len(self.myList)>0):
for i in range(len(self.myList)-1):
myVar = self.myList[i]
myKeys.append(myVar[1])
lis = game.active_entities.keys()
That seems suspicious to skip the last item.
FYI, I think the above code is equivalent to:
assert self.myList # Because if self.myList was empty, iterating over
# lis (later) would have caused an NameError.
myKeys = [value for key, value in self.myList[:-1]]
lis = game.active_entries.keys()
Related
My purpose is to change the value of the elements 3 and 4 to 4 and 3 and I have written a function that takes a list, first number and second number as arguments:
def pre9(the_list, value_to_replace, the_replacing_value):
for i in the_list:
if i == value_to_replace:
value_to_replace = the_replacing_value
elif i == the_replacing_value:
the_replacing_value = value_to_replace
return the_list
I then assign a test-case to a variabel and then print it:
test_pre9 = pre9([1,2,3,4,5,7,3,4], 3, 4)
print(test_pre9)
The result is: [1,2,3,4,5,7,3,4]
I expect it to be: [1,2,4,3,5,7,4,3]
I have for a long time ago written a code that accoplishes this task:
def uppgift_9():
the_list = [3,5,8,9,4,5]
for i in range(len(the_list)-1):
temp = the_list[3]
the_list[3] = the_list[4]
the_list[4] = temp
return the_list
But I've read in many places that using range(len()) is not "pythonic" and it is possible to do anything without using it.
Does anyone know why my code fails?
You don't actually change the item in the list, try this:
def pre9(the_list, value_to_replace, the_replacing_value):
for i, value in enumerate(the_list):
if value == value_to_replace:
the_list[i] = the_replacing_value
elif value == the_replacing_value:
the_list[i] = value_to_replace
return the_list
Now the list will have the actually items changed to what you wanted it to be. Enumerate() returns the index and value of an item in a list, it's very handy! And indeed, the range(len()) is not very pythonic and is usually used when people jump from other languages like Java, C# etc. Using enumerate() is the correct 'pythonic' way of achieving this.
I've been trying to turn the output of my list comprehension into a variable. Quite silly, but no matter what I try, I seem to end up with an empty list (or NoneType variable).
I'm guessing it has something to do with the generator that it uses, but I'm not sure how to get around it as I need the generator to retrieve the desired results from my JSON file. (And I'm too much of a list comprehension and generator newbie to see how).
This is the working piece of code (originally posted as an answer to these questions (here and here)).
I'd like the output of the print() part to be written to a list.
def item_generator(json_Response_GT, identifier):
if isinstance(json_Response_GT, dict):
for k, v in json_Response_GT.items():
if k == identifier:
yield v
else:
yield from item_generator(v, identifier)
elif isinstance(json_Response_GT, list):
for item in json_Response_GT:
yield from item_generator(item, identifier)
res = item_generator(json_Response_GT, "identifier")
print([x for x in res])
Any help would be greatly appreciated!
A generator keeps its state, so after you iterate through it once (in order to print), another iteration will start at the end and yield nothing.
print([x for x in res]) # res is used up here
a = [x for x in res] # nothing left in res
Instead, do this:
a = [x for x in res] # or a = list(res)
# now res is used up, but a is a fixed list - it can be read and accessed as many times as you want without changing its state
print(a)
res = [x for x in item_generator(json_Response_GT, "identifier")] should do the trick.
In the following Function I tried to simulate an wide search alike function.
First Function 'Hello' by giving and search name = V, is supposed to give you all the tuple partners that it has in a list.
Second Function is supposed to have two lists, first for already visited and the ones who are still in a list i.e. que.
With two for loops I went through newly generated list by the word given in, so that I can use those words to generate further tuple partners, that aren't in besucht list. After the usage has been done the item will be deleted from the 'Liste' and appended to besucht.
Question: It doesn't work as I intended and I don't understand why
V = {'CGN', 'FRA','KEL','MUC','PAD','SFX','STR','TXL'}
E = {('SFX','TXL'),('FRA','CGN'),('FRA','MUC'),('FRA','STR'),('CGN','FRA'),('STR','FRA'),('TXL','SFX'),('CGN','PAD'),('PAD','KEL'),('MUC','KEL'),('KEL','STR') }
S = {('A','B'),('A','B')}
def Hallo(V,E):
Total = []
Que = []
for i in E:
for j in i:
if j == V:
Total.append(i)
for i in Total:
for a in i:
if a != V:
if a not in Que:
Que.append(a)
return Que
def Durchsuchen(V,E):
besucht = []
Liste = []
Liste.append(Hallo(V,E))
besucht.append(V)
while len(Liste) !=0
for i in Liste:
if i not in besucht:
besucht.append(i)
Liste.remove(i)
Liste.append(Hallo(i,E))
print Liste
print besucht
print Durchsuchen('FRA',E)
What is supposed to do? It's supposed to give you all the possibilities i.e if you give in 'FRA' it will generate [MUC, STR, CGN] since MUC and etc is within this list this should also give you KEL for example. I.E all the possible options that are out there.
Well your problem is that you use remove() on a list inside a loop that iterates through it - which should never be done. This changes the logic that the loop relies on and causes your unexpected results.
The first function works just fine so I'd suggest you to use it once on your first word 'FRA', and then use it on every word that pairs with it - [MUC, STR, CGN] in your example. Then use the following code on the lists that you got:
newList = []
for ls in listOfLists:
for word in ls:
if word not in newList:
newList.append(word)
here is my code
def common_words(count_dict, limit):
'''
>>> k = {'you':2, 'made':1, 'me':1}
>>> common_words(k,2)
>>> k
{'you':2}
'''
new_list = list(revert_dictionary(count_dict).items())[::-1]
count_dict = {}
for number,word in new_list:
if len(count_dict) + len(word) <= limit:
for x in word:
count_dict[x] = number
print (count_dict)
def revert_dictionary(dictionary):
'''
>>> revert_dictionary({'sb':1, 'QAQ':2, 'CCC':2})
{1: ['sb'], 2: ['CCC', 'QAQ']}
'''
reverted = {}
for key,value in dictionary.items():
reverted[value] = reverted.get(value,[]) + [key]
return reverted
count_dict = {'you':2, 'made':1, 'me':1}
common_words(count_dict,2)
print (count_dict)
what i expected is to have the count_dict variable to change to {'you':2}.
It did work fine in the function's print statement, but not outside the function..
The problem, as others have already written, is that your function assigns a new empty dictionary to count_dict:
count_dict = {}
When you do this you modify the local variable count_dict, but the variable with the same name in the main part of your program continues to point to the original dictionary.
You should understand that you are allowed to modify the dictionary you passed in the function argument; just don't replace it with a new dictionary. To get your code to work without modifying anything else, you can instead delete all elements of the existing dictionary:
count_dict.clear()
This modifies the dictionary that was passed to the function, deleting all its elements-- which is what you intended. That said, if you are performing a new calculation it's usually a better idea to create a new dictionary in your function, and return it with return.
As already mentioned, the problem is that with count_dict = {} you are not changing the passed in dictionary, but you create a new one, and all subsequent changes are done on that new dictionary. The classical approach would be to just return the new dict, but it seems like you can't do that.
Alternatively, instead of adding the values to a new dictionary, you could reverse your condition and delete values from the existing dictionary. You can't use len(count_dict) in the condition, though, and have to use another variable to keep track of the elements already "added" to (or rather, not removed from) the dictionary.
def common_words(count_dict, limit):
new_list = list(revert_dictionary(count_dict).items())[::-1]
count = 0
for number,word in new_list:
if count + len(word) > limit:
for x in word:
del count_dict[x]
else:
count += len(word)
Also note that the dict returned from revert_dictionary does not have a particular order, so the line new_list = list(revert_dictionary(count_dict).items())[::-1] is not guaranteed to give you the items in any particular order, as well. You might want to add sorted here and sort by the count, but I'm not sure if you actually want that.
new_list = sorted(revert_dictionary(count_dict).items(), reverse=True)
just write
return count_dict
below
print count_dict
in function common_words()
and change
common_words(count_dict,2)
to
count_dict=common_words(count_dict,2)
So basically you need to return value from function and store that in your variable. When you are calling function and give it a parameter. It sends its copy to that function not variable itself.
Below is a simple function to remove duplicates in a list while preserving order. I've tried it and it actually works, so the problem here is my understanding. It seems to me that the second time you run uniq.remove(item) for a given item, it will return an error (KeyError or ValueError I think?) because that item has already been removed from the unique set. Is this not the case?
def unique(seq):
uniq = set(seq)
return [item for item in seq if item in uniq and not uniq.remove(item)]
There's a check if item in uniq which gets executed before the item is removed. The and operator is nice in that it "short circuits". This means that if the condition on the left evaluates to False-like, then the condition on the right doesn't get evaluated -- We already know the expression can't be True-like.
set.remove is an in-place operation. This means that it does not return anything (well, it returns None); and bool(None) is False.
So your list comprehension is effectively this:
answer = []
for item in seq:
if item in uniq and not uniq.remove(item):
answer.append(item)
and since python does short circuiting of conditionals (as others have pointed out), this is effectively:
answer = []
for item in seq:
if item in uniq:
if not uniq.remove(item):
answer.append(item)
Of course, since unique.remove(item) returns None (the bool of which is False), either both conditions are evaluated or neither.
The reason that the second condition exists is to remove item from uniq. This way, if/when you encounter item again (as a duplicate in seq), it will not be found in uniq because it was deleted from uniq the last time it was found there.
Now, keep in mind, that this is fairly dangerous as conditions that modify variables are considered bad style (imagine debugging such a conditional when you aren't fully familiar with what it does). Conditionals should really not modify the variables they check. As such, they should only read the variables, not write to them as well.
Hope this helps
mgilson and others has answered this question nicely, as usual. I thought I might point out what is probably the canonical way of doing this in python, namely using the unique_everseen recipe from the recipe section of the itertools docs, quoted below:
from itertools import ifilterfalse
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
seen_add = seen.add
if key is None:
for element in ifilterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
def unique_with_order(seq):
final = []
for item in seq:
if item not in final:
final.append(item)
return final
print unique_with_order([1,2,3,3,4,3,6])
Break it down, make it simple :) Not everything has to be a list comprehension these days.
#mgilson's answer is the right one, but here, for your information, is a possible lazy (generator) version of the same function. This means it'll work for iterables that don't fit in memory - including infinite iterators - as long as the set of its elements will.
def unique(iterable):
uniq = set()
for item in iterable:
if item not in uniq:
uniq.add(item)
yield item
The first time you run this function, you will get [1,2,3,4] from your list comprehension and the set uniq will be emptied. The second time you run this function, you will get [] because your set uniq will be empty. The reason you don't get any errors on the second run is that Python's and short circuits - it sees the first clause (item in uniq) is false and doesn't bother to run the second clause.