I am studying for my PCEP and in the practice set there is a syntax for referencing lists I don't understand. Given:
numbers = [2, 3, 5, 8]
print(numbers[numbers[0]])
print(numbers[0])
The first print statement I can't understand, how am I getting 5 as the output? The second statement makes sense, it's how I have done it in the past which is index 0 but what is the first print statement doing? Calling a list within a list like a nested list? And how am I getting to index 2 to get the output of 5?
Thanks in advance for your help/time, it's much appreciated.
I've tried changing the refrenced index and counting through the list twice but often times I am seeing an index out of range response, or a number I wasn't expected. e.g, setting the print statement to
print(numbers[numbers[1]])
Gives me the output of '8' and I don't understand how I am to get there with the index of 1.
It is a similar principle as the parentheses in math. If you have nested parentheses you work from the inside out. In the first example the code solves the inner most number[] list.
So number[0] is 2. Then the code use this converted code to solve the next outer list so you can see it as number[2], instead of number[number[0]].
So number[2] is 5.
When you have nested expressions and they're confusing you, the easiest way to understand what's happening is to break it up into multiple statements.
print(numbers[numbers[0]])
is equivalent to
temp = numbers[0]
print(numbers[temp])
The first statement sets temp = 2, so the second statement prints numbers[2], which is 5.
Related
I'm trying to enter a 100 number square into a 2D list https://i.stack.imgur.com/eSFiO.png with each list containing 10 numbers 1-10,11-20,21-30 and so on. This is my code so far but when I run it in my editor it just keeps running and eventually crashes without printing anything. Please explain to me what I am doing wrong. Thanks
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[1,2,3,4,5,6,7,8,9,10]
for row in number_square:
for number in number_list:
number_square.append(number)
number_list.remove(number)
number_list.append(number+10)
print(number_square)
That's because you aren't accessing the content of neither row nor number in the for loops. Here is a suggestion:
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[1,2,3,4,5,6,7,8,9,10]
i = 0
for row in number_square:
for i in range(len(number_list)):
row.append(number_list[i])
number_list[i] += 10
print(number_square)
Note that the first loop is equivalent to for each. In this syntax, you can't alter the value of the item in a list. In the second loop, I put a "traditional" for loop with range to alter the values in number_list.
There are many changes required in your code. Good effort from your side on trying the code. I have modified the code and probably you can get the logic from it.
number_square=[[],[],[],[],[],[],[],[],[],[]]
number_list=[10,20,30,40,50,60,70,80,90,100]
for i in range(0,len(number_square)):
for j in range(number_list[i]-9,number_list[i]+1):
number_square[i].append(j)
Problems:
Removing from number_list while iterating over it
Appending to number_square instead of row
If I were you, I'd use a list comprehension with ranges instead:
number_square = [list(range(i, i+10)) for i in range(1, 101, 10)]
Due credit to Onyambu for suggesting something similar in a comment
I have been getting the memory error but unable to understand the reason behind that. Below is the code. Using the list and appending just two more elements in the list.
mylist = ['phonon', 'communication']
for i in mylist:
mylist.append(i.upper())
print(mylist)
It will be really very helpful if anyone can help me with that.
for i in mylist:
mylist.append(i.upper())
is basically appending to your list while iterating on it. So the iteration never ends.
You want to do:
mylist += [i.upper() for i in mylist]
in that last case, the right-hand expression is computed from a non-modified mylist, then its elements are appended to the existing mylist.
note that the above is equivalent to
mylist.extend([i.upper() for i in mylist])
or
mylist.extend(list(map(str.upper,mylist)))
note that for both snippets above it is necessary to convert to list, or you get the same memory error if the inside is lazily evaluated. The last snippet is probably the fastest because it doesn't use any python loops at all, map, and no lambda
For all 3 snippets the result is:
['phonon', 'communication', 'PHONON', 'COMMUNICATION']
I would add your edited elements to a new list:
uppers = []
for i in mylist:
uppers.append(i.upper())
print(uppers)
Building off Jean's answer:
mylist.extend(i.upper() for i in mylist)
Should give you the desired result.
When I run the following code, it prints. However, I expected only one 1 rather than two.
for i in (1,1):
print(i)
Output
1
1
You are iterating over a tuple which contains two elements with value 1 so it prints 1 twice. Your code is equivalent to:
list = [1, 1]
for item in list:
print(item)
If you want to loop over a range of numbers:
for i in range(1, 2):
print(i)
Or if you want to print unique numbers or values in list or tuple convert it into the set it will automatically remove the duplicates
newList = set(list)
for value in newList:
print(value)
Sets and tuples are different. I suspect you are confusing them. On a set:
for i in {1, 1}:
print(i)
1
On a tuple:
for i in (1, 1):
print(i)
1
1
Think of sets as being like sets in math, and tuples as being more like sequences - you can have redundancies in a sequence, but not in a set.
After reading #KeshavGarg's answer, I suspect you thought that (a,b) in Python would mean stuff in a through b. As you're probably aware by now, this is not the case - you need range to get that. Interestingly (and I admit tangentially), the syntax we're discussing here varies by language. In MATLAB, the range syntax looks a lot more like what I assume you thought the Python range syntax was:
>> for i=1:4
disp(i)
end
There has been some discussion of implementing range literals (a la Matlab) in Python. This introduces a variety of interesting new problems, which you can read about in the documentation linked in the previous sentence.
For loops are always inclusive in Python: they always run over all elements of the iterator (other than exceptions such as break, etc.). What probably has you confused is the range syntax. range(1,1) will create a range object with one element. It is the range function, not the for-loop, that is exclusive, in the sense of not including the stop argument in the range object.
what I need right now is to extract a specific file
example:
List = [1,2,3,4,5,6]
print(List[1:2])
Output being [2]
what I need is exactly 2, and not [2]
To python in general, anything will help thanks
In [1]: List = [1,2,3,4,5,6]
In [2]: print(List[1])
2
When you are slicing an array OR list, it means you giving a range of index like [1:5] so, it must return a list. Better you can use single index of array OR list. Then it will return single instance.
Well the easiest way to do this is to just print the value of what you want. I.E:
list = [1,2,3,4,5,6,7,8,9]
print(list[1])
This will print out 2. I am confused on why you are looking in a range of the list which will output another list hence why you are getting [2]. I recommend going back and reading up on lists and what you can do to them in the python tutorials. Youtube might be a great help if your more visual as well. Best of luck
P.S. Remember that lists are 0 based and start at 0. Thus to get 2 in this list, you need to print out the 1 spot which is really the second spot in the list. 0 = 1, 1 = 2, 2 = 3, etc if you look at the list I created.
My function takes a number, and a list of numbers.
If 2 numbers in the list add up to the original number, in the form [Num1, Num2].
Now I don't want any "duplicates" i.e. I only want [4, -7] returned and not [4, -7], [-7, 4].
def pairs(n, num_list):
newest_list = []
for j in range(len(num_list)):
for i in range(len(num_list)-1):
if num_list[j] + num_list[i+1] == n:
newest_list.append([num_list[j], num_list[i+1]])
return newest_list
Now I'd like a hint rather than code posted, a simple.
My question is:
Do I have the ability to do that within my code, and if so, a hint would be great, or will I need to define another function to do that for me?
You definitely have the ability to do that within your code.
A hint to complete this would be to think about at what point in your code it makes sense to stop searching for further matches and to return what you've found. Let me know if that's too cryptic!
You can still do that in your current code by simply appending these two numbers into a Set. For more info, this will help you.
if you have 2 lists l1, and l2 where:
l1=[1,2]
l2=[2,1]
If you convert them to sets, you can compare them and they will evaluate to True if they have the same elements, no matter what the order is:
set(l1) == set(l2) # this evaluates to True
In your if condition, before appending the numbers, you can check if the set set([num_list[j], num_list[i+1]]) is already in newest_list.
I am tempted to write some code, but you said not to, so I'll leave it here :p
You can leave your code the way it is, but before you return the list, you can filter the list with a predicate that the pair [a,b] is only accepted if pair [b,a] is not in the list
When adding a pair [a, b] to the result list, sort the pair, then see if it's in the result list. If so, don't add it.
Also, consider using a Python set.