My print function syntax is causing an error in python 3 - python

I have a list of lists with tuples. I want to get the length of a tuple using:
item1=(4, 8, 16, 30)
list6=[[(4, 8, 16, 29)], [(4, 8, 16, 30)], [(4, 8, 16, 32)]]
print("list6.index((4, 8, 16, 29)):",list6.index([item1]))
print("len(list6[1]):"), len(list6[1])
Output:
list6.index((4, 8, 16, 29)): 1
len(list6[1]):
There is no value for len(list6[1]). Can someone show me the correct syntax for this?

The code works fine in Python 2. If you are using Python 3, there is an issue with last line, because print is a function. So, because of where you've put the close parenthesis, only the first part is actually passed to print. Try this instead
print("len(list6[1]):", len(list6[1]))

Related

For loop or Lambda function?

I have this two code, they both (only the first right now) should return me the lenght of sequence that is repeated (13, 6, 6, 14, 6 AND 13, 6, 6, 14, 6):
l = [13, 6, 6, 14, 6, 13, 6, 6, 14, 6]
def count_period(l):
l_max = int((len(l)/2)+1)
for i in range(2, l_max):
interval1 = l[0:i]
intervallo2 = l[i:2*i]
if interval1 == interval2:
return i
And:
l = [13, 6, 6, 14, 6, 13, 6, 6, 14, 6]
l_max = int((len(l)/2)+1)
period_value= next(filter(lambda x: [l[0:x] == l[x:2*x] in range(2, l_max)], l), None)
Why the first code return the right value (5) while the second return me 13? What I'm doing wrong? About this topic: since I'm newbie to Python from a general point of view (and not about this case), how I can know which path should i follow to get a good code (with low execution time and low cyclomatic complexity)? For example in this case (let us assume that I haven't written anything yet), which of this two way (or another) should I follow? Thank you!
You should be filtering range(2, l_max) not l to translate your code from the top into a filter. Right now for each value in l you are creating a list in the predicate lamdba you define filter to use. If that list has items in it it returns true, only the empty list would return false. Filter then decides to keep the value x if it passes the predicate or discards it if it does not. So therefore it keeps all items in the original list and next on that list is 13. You should do this instead:
l = [13, 6, 6, 14, 6, 13, 6, 6, 14, 6]
l_max = int((len(l)/2)+1)
period_value= next(filter(lambda x: l[0:x] == l[x:2*x], range(2, l_max)), None)

found this sample code tried to run it but it did not work

my_favorite_numbers = [4, 8, 15, 16, 42]
for number in my_favorite_numbers:
my_favorite_numbers.append(1)
THE COMPILER IS NOT SHOWING ANY RESULTS I TRIED TO USE CMD , PYCHARM,SUBLIME AND AN ONLINE ONE TOO
add print function in your code
print(my_favourite_numbers)
and you can check python official tutorial
You are running an infinite loop. I assume you need something like that:
my_favorite_numbers = [4, 8, 15, 16, 42]
for number in range(len(my_favorite_numbers)):
my_favorite_numbers.append(1)
print(my_favorite_numbers)
Output: [4, 8, 15, 16, 42, 1, 1, 1, 1, 1]
Although the goal of this code is still not clear to me.

How to do i print some numbers using .sample() from the random built in module in python

I working on a problem where I'm supposed to generate ten random but unique numbers that range from 1 to 15 inclusive. The thing is, I'm supposed to write everything in one line and to also get this output:
[2, 4, 6, 7, 8, 9, 11, 12, 13, 15]
Below I have some code I wrote but, it's not getting the output I want. What am I doing wrong and can I perhaps see a solution with a break so I know how to do this going down the road?
import random
print(sorted(random.sample(range(1,16),15)))
Output:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
The output I want is:
[2,4,6,7,8,9,11,12,13,15]
How do I get this in one line of code?
>>> help(random.sample)
sample(population, k): method of random.Random instance
Chooses k unique random elements from a population sequence or set.
I'm supposed to write everything in one line and to also get this output:
[2, 4, 6, 7, 8, 9, 11, 12, 13, 15]
>>> sorted(__import__('random').Random(4225).sample(range(1, 16), 10))
[2, 4, 6, 7, 8, 9, 11, 12, 13, 15]
If you want to generate ten numbers in range 1-15, change
print(sorted(random.sample(range(1,16),15)))
to
print(sorted(random.sample(range(1,16),10)))
# From the documentation :
# random.sample(population, k)
import random
population = range(16)
how_may_sample = 10
random.sample(population, how_many_sample)
# Now in one line
random.sample(range(16), 10)

How do I make this into a for loop?

So basically I am trying to replace this:
board = {
0:[0, 1, 2, 9, 10, 11, 18, 19, 20],
1:[3, 4, 5, 12, 13, 14, 21, 22, 23],
2:[6, 7, 8, 15, 16, 17, 24, 25, 26]}
with a for loop that will automatically create it. Sorry if this seems obvious, but I'm a bit of a noob and I'm having a lot of trouble with this.
It looks like you're generating the first 27 integers (starting at 0) and then grouping them. Let's write it like that.
def group_by_threes(n=27, group_count=3):
# The end result will be a dict of lists. The number of lists
# is determined by `group_count`.
result = {}
for x in range(group_count):
result[x] = []
# Since we're using subgroups of 3, we iterate by threes:
for x in range(n // 3):
base = 3 * x
result[x % 3] += [base, base + 1, base + 2]
# And give back the answer!
return result
This code could be made better by making the size of groups (three in this case) an argument, but I'll leave that as an exercise to the reader. ;)
The advantage of this method is that it's much more modular and adaptable than just writing a one-off method that generates the exact list you're looking for. After all, if you only wanted to ever generate that one list you showed, then it'd probably be better to hardcode it!
def create_list(x):
a = [x,x+1,x+2,x+9,x+9+1,x+9+2,x+18,x+18+1,x+18+2]
return a
output = {}
for i in range(3):
output[i*3] = create_list(i*3)
print output
please try this you get desired output
def create_list(x):
res = []
for i in xrange(0,3):
for j in xrange(0,3):
res.append(3*x+ 9*i + j)
return res
dictionary={}
for i in xrange(0,3):
dictionary[i]=create_list(i)
print dictionary
Result:
{0: [0, 1, 2, 9, 10, 11, 18, 19, 20], 1: [3, 4, 5, 12, 13, 14, 21, 22, 23], 2: [6, 7, 8, 15, 16, 17, 24, 25, 26]}

How do I enable 'doctest.ELLIPSIS' at the Python prompt?

I often see examples that use doctest.ELLIPSIS to limit output in interactive examples of Python use,
>>> print range(20) # doctest:+ELLIPSIS
[0, 1, ..., 18, 19]
and see here how to enable the feature in modules; but I can't figure out how to enable this feature interactvely.
How do I enable doctest.ELLIPSIS at the Python or IPython prompt?
It's not limiting the output, it's telling doctest it doesn't need to check all of it. That line of code will still produce the full output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
But doctest will only check the bits before and after the ....
I don't know of anything to limit the output like that in interactive sessions, though if you use Python 3, you could write your own implementation of print() to do it.

Categories

Resources