Reputation: 63
I am doing an exercise in Python and I am having a problem appending the IP address with the count var.
Vast explanation. First, here is the code:
if c == 4:
temp = IP
for address in IP:
count = temp.count(address)
c = str(count)
a = address
while count != 0:
temp.remove(address)
count -= 1
c = a + "|" + c
temp.append(c)
print temp
func(IP)
Okay...so: Whenever the user inputs 4, the function creates the list 'temp', and compares it with the list 'IP', which is a like a master list (I am not familiar with English coding shortcuts) that the function receives from the program itself. Afterwards, it creates a loop in which it creates a count var to count the times the IP address is presented inside the temp list (IP list), then it converts the count var into a string and stores the address inside the var a. After all this, it creates another loop to remove all instances of that address in the temp list, and in the end, it appends the same address with the stringed count in it.
Logically, everything is fine isn't it? (at least I think so...) but here is the output if I insert: 10.0.0.1, 10.0.0.3, 10.0.0.3, 10.0.0.5 -
['10.0.0.5', '10.0.0.1|1', '10.0.0.3|2|1']
As you can see, it should be: ['10.0.0.5|1', '10.0.0.1|1', '10.0.0.3|2'] right?
So what's the problem? I really need help with this one...Hope I explained myself well enough! Thanks a lot guys!
Upvotes: 1
Views: 68
Reputation: 1496
Your code shows a few misunderstandings about Python and programming, so let me go over a couple and see if I can I help you out.
When you assign temp = IP
, I assume you are trying to make a copy of IP
, however they are still one and the same. Any change to temp is also a change to IP
. This means that you are adding and removing items from IP
while iterating over it when you change temp
.
In Python some things are mutable, like lists and dictionaries. When you pass something mutable into a function and change it inside the function it is a also affected outside the function. When you set something equal to it, you now have two names for the same list or dictionary. If something is immutable, like a set or an integer, this does not happen.
You use variables in your code to store intermediate values, but do not name them in a way that makes the code more readable. How many things do you have to keep track of? In your code you have about 6 variables you have to keep track of. That is a lot to remember. Also the variable names do little to help. What is c
? It is user input at one point, the count as a string at another point and the result string at another. This is sure to confuse you and anyone who reads your code. Try to use descriptive variable names, use intermediate values only when they make the code clearer, and don't reuse the same variable for completely different tasks.
address_counts = {} # Create an empty dictionary to map addresses to the number of times they occur
for address in IP:
if address not in address_counts:
address_counts[address] = 1 # The first time we see an address set the count to 1
else:
address_counts[address] += 1 # The next time we see something, add 1 to the count
for address, count in address_count.iteritems(): # Python 2 specific, loops over each key and value in a dictionary. In Python 3, use .items()
print address + '|' + str(count)
Upvotes: 1