Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am breaking down the following problem:
def removeElement(nums, val):
for i in range(nums.count(val)):
nums.remove(val)
return len(nums)
I cannot understand what for i in range(nums.count(val)): does
Thx
nums.count(val) counts how many instances of val are in nums.
for i in range(nums.count(val)):... is a loop that iterates nums.count(val) times, or, in other words, it iterates as many times as the number of val's in nums.
i does not actually do anything here, so the more "Pythonic" way of writing that would be to replace i with an underscore (_) to indicate that the variable is not used.
Finally, nums.remove(val) removes the first occurence of val from nums. This is repeated num.count(val) times, so this loop is actually removing all instances of val from num.
return len(nums) simply returns the number of elements in nums after all the val's are removed, though it is wrongly indented (should be outside of the for loop).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.
res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
import random
for i in range(5):
print(random.randint(1, 10))
Is it the number of integers that we want to print? But we didn't specify that it's the number of integers in the code, so how does python understand?
The Python for construct requires a variable name between for and in. Conventional practice is to use _ (underscore) as the variable in cases where a variable is required but not actually used/relevant. Note that _ is a valid variable name.
for i in range(5):
do this action
Is (the Python way of saying
"for each element in range(5)
do this action".
range(5) can be replaced by any iterable collection.
In this example the variable i is not used. We might write
for i in range(5):
print(i)
which would print out all the values from the expression range(5).
As you guessed, i in that code will be the number of random integers to be printed. That is because, in python, the range constructor will generate a sequence of integers when specified in the way you are showing.
If only one argument is specified, python will assume that you want to begin by the number zero, incrementing by one unit until it reaches the number one unit below the specified argument.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to remove a certain element from a list of dictionaries. Unfortunately, the only reference to the object needing removal is a place in another list:
enemy_list[num]
I need to take out enemy_list[num] from everyone.
ii = 0
for i in enemy_list:
print(ii, ':', enemy_list[ii])
ii += 1
num = input("Which of your opponents would you like to eliminate? ")
num = int(num)
del everyone[enemy_list[num]]
I cannot remove it from only enemy_list because it is reset multiple times as everyone in list everyone takes a turn.
I tried this question, but it only worked from removing entire lists from eachother. Is there a way I could change it to work for this case?
Thanks for your time!
everyone.remove(enemy_list[num])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I want to do something like this
"(1.0)" which returns ["1","0"]
similarly "((1.0).1)" which returns ["(1.0)", "1")
How do i do this python? Thanks for the help
so basically I want to break the string "(1.0)" into a list [1,0] where the dot is the separator.
some examples
((1.0).(2.0)) -> [(1.0), (2.0)]
(((1.0).(2.0)).1) -> [((1.0).(2.0)), 1]
I hope this is more clear.
Here is my version:
def countPar(s):
s=s[1:-1]
openPar=0
for (i,c) in enumerate(s):
if c=="(":
openPar+=1
elif c==")":
openPar-=1
if openPar==0:
break
return [s[0:i+1],s[i+2:]]
You'll need to build a little parser. Iterate through the characters of the string, keeping track of the current nesting level of parentheses. Then you can detect the . you care about by checking that first, the character is a ., and second, there's only one level of parentheses open at that point. Then just place the characters in one buffer or another depending on whether you've reached that . or not.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to combine the two statements inside 'for' loop.
num_pro=raw_input("ENTER THE NUMBER OF PRODUCTIONS: ")
right=[];left=[];
for i in range(int(num_pro)):
l,r=raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->')
right.append(r);left.append(l)
sample input: E->abc
Append tuples to one list, then split out the lists using zip():
entries = []
for i in range(int(num_pro)):
entries.append(raw_input("ENTER PRODUCTION"+str(i+1)+" : ").split('->'))
left, right = zip(*entries)
zip(*iterable) transposes the nested list; columns become rows. Because you have two 'columns' (pairs of values), you end up with two rows instead.
Not without making it more complex. Each method needs to be called individually, and the only way to do that is either explicitly, as you have done, or in a loop.
If you are willing to store the whole production (which isn't necessarily a bad idea, since it keeps both sides synchronized) then just append the split result instead.
productions = []
for ...
productions.append(....split('->'))