def cammino(L,inizio,fine,percorso):
g=grafo(L)
percorso=percorso+[inizio]
#print(percorso)
#set_stato(L,inizio,'V')
if inizio==fine:
return percorso
if inizio not in L:
print('Cella',inizio,'non presente')
return None
if fine not in L:
print('Cella',fine,'non presente')
return None
for nodo in g[inizio]:
if nodo not in percorso:
#set_stato(L,nodo,'V')
newpath=cammino(L,nodo,fine,percorso)
if newpath:
return newpath
return []
Hi everyone, what does "if newpath:" means?
My newpath is a list. I don't understand what does the "if" do to a list
if, as elsewhere in Python, applied to a list tests if it's empty or not.
>>> a = []
>>> b = ["foo", "bar"]
>>> if a:
... print a
... elif b:
... print b
...
['foo', 'bar']
This is true even if all of the elements of the list are falsy:
>>> c = [False]
>>> if c:
... print "xyzzy"
...
xyzzy
See the documentation and on truth testing for more information.
Related
How can I print out the list of "foods"?
All I can print was something like memory location.
class Fridge:
isOpened = False
foods = []
def open(self):
self.isOpened = True
print "Fridge open"
def put(self, thing):
if self.isOpened:
self.foods.append(thing)
print 'Food in'
else:
print 'Cannot do that'
def close(self):
self.isOpened = False
print 'Fridge closed.'
def __repr__(self):
return "%s" % (self.foods[0])
class Food:
pass
The way you defined repr(), it will only produce the first item in the list foods (as indicated by foods[0]). Furthermore, if the list foods is empty, calling repr() will result in an IndexError.
One way to print the list would be:
def __repr__(self):
return str([food for food in self.foods])
If you are not familiar with the syntax, please check out List Comprehensions.
Here's an example use case of your class:
>>> f = Fridge()
>>> f.open()
Fridge open
>>> f.put("Fruit")
Food in
>>> f.put("Vegetables")
Food in
>>> f.put("Beer")
Food in
>>> print f
['Fruit', 'Vegetables', 'Beer']
How can I extract '1' '11' and '111' from this list ?
T0 = ['4\t1\t\n', '0.25\t11\t\n', '0.2\t111\t\n']
to extract '4', '0.25' and '0.2' I used this :
def extract(T0):
T1 = []
for i in range(0, len(T0)):
pos = T0[i].index('\t')
T1.append(resultat[i][0: pos])
return T1
then I got :
T1 = ['4','0.25','0.2']
but for the rest I don't know how to extract it
can you help me please?
Using your code as base, it can be done as below. Will return as string if its alphabet, otherwise return as decimal integer.
def extract(T0):
T1=[]
for i in range len(T0):
tmp = T0[i].split('\t')[1]
if tmp.isalpha():
T1.append(tmp)
else:
T1.append(int(tmp))
return T1
Alternatively, try below for a more compact code using list comprehension
def extract(T0):
# return as string if its alphabet else return as decimal integer
# change int function to float if wanna return as float
tmp = [i.split('\t')[1] for i in T0]
return [i if i.isalpha() else int(i) for i in tmp]
Example
T0= ['X\tY\tf(x.y)\n', '0\t0\t\n', '0.1\t10\t\n', '0.2\t20\t\n', '0.3\t30\t\n']
extract(T0) # return ['Y', 0, 10, 20, 30]
You can accomplish this with the re module and a list comprehension.
import re
# create a regular expression object
regex = re.compile(r'[0-9]{1,}\.{0,1}[0-9]{0,}')
# assign the input list
T0 = ['4\t1\t\n', '0.25\t11\t\n', '0.2\t111\t\n']
# get a list of extractions using the regex
extractions = [x for x in [re.findall(regex, e) for e in T0]]
print extractions
# => [['4', '1'], ['0.25', '11'], ['0.2', '111']]
I am working on a search algorithm in python but there is something I don't get to work..
I have a list which looks like this [["A","1.txt"],["A","2.txt"],["A","3.txt"],["B","1.txt"],["B","3.txt"]]
Now I want to merge the sub-lists that have the same first index. So the result would be:
[["A",["1.txt","2.txt",3.txt"]],["B",["1.txt"],["3.txt"]]]
Anyone who knows how to do this...
Kinda got a sort (on mergesort basis) but this does not merge the tuples
def merge_pairs(data):
if len(data) <= 1 :
return data[:]
else:
mid = len(data) // 2
fst = merge_pairs(data[:mid])
snd = merge_pairs(data[mid:])
res = []
fi = 0
si = 0
while fi < len(fst) and si < len(snd):
if fst[fi][0] < snd[si][0] or fst[fi][0] == snd[si][0] and fst[fi][1] < snd[si][1]:
res.append(fst[fi])
fi = fi + 1
else:
res.append(snd[si])
si = si + 1
if fi < len(fst) :
res.extend(fst[fi:])
elif si < len(snd) :
res.extend(snd[si:])
return res
So i'd like not to use the dict() function of python
Thanks in advance
The easiest way (which may or may not be slower than the hard way) is to use a defaultdict:
>>> from collections import defaultdict
>>> result = defaultdict(list)
>>> mylist = [["A","1.txt"],["A","2.txt"],["A","3.txt"],["B","1.txt"],["B","3.txt"]]
>>> for key, value in mylist:
... result[key].append(value)
...
>>> print(sorted(result.items()))
[('A', ['1.txt', '2.txt', '3.txt']), ('B', ['1.txt', '3.txt'])]
The hard way (if your data is truly already sorted):
>>> src = [["A","1.txt"],["A","2.txt"],["A","3.txt"],["B","1.txt"],["B","3.txt"]]
>>> prev = None
>>> dst = []
>>> for key, value in src:
... if key != prev:
... prev = key
... dst.append((key, []))
... dst[-1][-1].append(value)
...
>>> print(dst)
[('A', ['1.txt', '2.txt', '3.txt']), ('B', ['1.txt', '3.txt'])]
But note that Python sort is really, really fast, and Python loops like this... Not so much.
Edit According to your comment below, you also want counts. Again there is a dictionary way:
>>> from collections import defaultdict
>>> result = defaultdict(lambda: defaultdict(int))
>>> mylist = [["A","1.txt"],["A","2.txt"],["A", "2.txt"],["A","3.txt"],["B","1.txt"],["B","3.txt"]]
>>> for key, value in mylist:
... result[key][value] += 1
...
>>> print(sorted((x, sorted(y.items())) for (x, y) in result.items()))
[('A', [('1.txt', 1), ('2.txt', 2), ('3.txt', 1)]), ('B', [('1.txt', 1), ('3.txt', 1)])]
and a loop way:
>>> src = [["A","1.txt"],["A","2.txt"],["A", "2.txt"],["A","3.txt"],["B","1.txt"],["B","3.txt"]]
>>> prevkey, prevvalue = None, None
>>> dst = []
>>> for key, value in src:
... if key != prevkey:
... prevkey = key
... prevvalue = None
... dst.append((key, []))
... if value != prevvalue:
... prevvalue = value
... dst[-1][-1].append([value, 0])
... dst[-1][-1][-1][-1] += 1
...
>>> dst
[('A', [['1.txt', 1], ['2.txt', 2], ['3.txt', 1]]), ('B', [['1.txt', 1], ['3.txt', 1]])]
You'd really want to run timeit to be sure, but in this instance, the loop way almost looks guaranteed to be slower (and of course, the dictionary way doesn't require you to do a pre-sort.)
In python I want to make a function that returns true if something is inside a list. Here is my example code.
def isin(List, value):
try:
i = List.index(value)
except ValueError:
return False
return True
For example in if I do
myList = [0,1,'string', 4.8]
isin(myList, 1) # I want to return True
isin(myList, 'animal') # I want to return False
Yes.
if 1 in myList # true
if "animal" in myList # false
Python has the in operator built-in for that:
myList = [0,1,'string', 4.8]
if 1 in myList:
# Do something
pass
print('animal' in myList) # Prints 'False'.
I wrote the following code for computing character bigrams and the output is right below. My question is, how do I get an output that excludes the last character (ie t)? and is there a quicker and more efficient method for computing character n-grams?
b='student'
>>> y=[]
>>> for x in range(len(b)):
n=b[x:x+2]
y.append(n)
>>> y
['st', 'tu', 'ud', 'de', 'en', 'nt', 't']
Here is the result I would like to get:['st','tu','ud','de','nt]
Thanks in advance for your suggestions.
To generate bigrams:
In [8]: b='student'
In [9]: [b[i:i+2] for i in range(len(b)-1)]
Out[9]: ['st', 'tu', 'ud', 'de', 'en', 'nt']
To generalize to a different n:
In [10]: n=4
In [11]: [b[i:i+n] for i in range(len(b)-n+1)]
Out[11]: ['stud', 'tude', 'uden', 'dent']
Try zip:
>>> def word2ngrams(text, n=3, exact=True):
... """ Convert text into character ngrams. """
... return ["".join(j) for j in zip(*[text[i:] for i in range(n)])]
...
>>> word2ngrams('foobarbarblacksheep')
['foo', 'oob', 'oba', 'bar', 'arb', 'rba', 'bar', 'arb', 'rbl', 'bla', 'lac', 'ack', 'cks', 'ksh', 'she', 'hee', 'eep']
but do note that it's slower:
import string, random, time
def zip_ngrams(text, n=3, exact=True):
return ["".join(j) for j in zip(*[text[i:] for i in range(n)])]
def nozip_ngrams(text, n=3):
return [text[i:i+n] for i in range(len(text)-n+1)]
# Generate 10000 random strings of length 100.
words = [''.join(random.choice(string.ascii_uppercase) for j in range(100)) for i in range(10000)]
start = time.time()
x = [zip_ngrams(w) for w in words]
print time.time() - start
start = time.time()
y = [nozip_ngrams(w) for w in words]
print time.time() - start
print x==y
[out]:
0.314492940903
0.197558879852
True
Although late, NLTK has an inbuilt function that implements ngrams
# python 3
from nltk import ngrams
["".join(k1) for k1 in list(ngrams("hello world",n=3))]
['hel', 'ell', 'llo', 'lo ', 'o w', ' wo', 'wor', 'orl', 'rld']
Ths fucntion gives you ngrams for n = 1 to n:
def getNgrams(sentences, n):
ngrams = []
for sentence in sentences:
_ngrams = []
for _n in range(1,n+1):
for pos in range(1,len(sentence)-_n):
_ngrams.append([sentence[pos:pos+_n]])
ngrams.append(_ngrams)
return ngrams