- 1
- 2
- 3
>>> mylist = [3,5,8,5,3,12]
>>> print dict().fromkeys(mylist).keys()
[8, 3, 12, 5]
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−162
>>> mylist = [3,5,8,5,3,12]
>>> print dict().fromkeys(mylist).keys()
[8, 3, 12, 5]
"Getting rid of duplicate items in a list" типа способ убрать дубликаты в списке
−171
#param - строка
if(param[0]=='"' and param[-1]=='"'):
param = param.strip('"')
else:
param = param.strip("'")
Приближенный алгоритм убирания кавычек. И ведь работает... как правило.
−168
piarra=[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9]
piarrb=[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9]
result = []
for i in range(len(piarra)**2):
result.append(0)
def decr(a):
if a[-1]>0:
a[-1]-=1
else:
a.pop()
decr(a)
a.append(9)
if a[0]==0:
a.remove(0)
while piarra != [0]:
decr(piarra)
for i in range(len(piarrb)):
result[-1-i]+=piarrb[-1-i]
while(sorted(result)[-1]>9):
for i in range(len(result)):
if result[i]>9:
result[i]-=10
result[i-1]+=1
while result[0]==0:
result.remove(0)
sqrstr=''
for i in result:
sqrsrt+=`i`
print sqrstr
Пи в квадрате. Оцените нетривиальность решений.
−155
while True:
if True:
return firstLevel()
−146
for section in config.sections():
GROUP_PREFIX = 'group '
if not section.startswith(GROUP_PREFIX):
continue
group = section[len(GROUP_PREFIX):]
if group in seen:
continue
из проекта gitosis
−151
try:
if respond[0] == 'command':
self.servercommands.append(respond)
else:
raise KeyError('Invalid type')
except (TypeError, IndexError, KeyError):
self.responds.append(respond)
−142
for i in range(4, len(l)):
if l[i] - l[i - 1] == 1 and l[i - 1] - l[i - 2] == 1 and l[i - 2] - l[i - 3] == 1 and l[i - 3] - l[i - 4] == 1:
return True
Нужно было найти в списке 5 элементов, которые могут начинаться на любой позиции, но должны идти по порядку (например [9, 10, 11, 12, 13]). Не придумал ничего лучше :(
−149
def i1iII1IiiIiI1 ( ) :
ii [ 5 ] . executebuiltin ( "xbmc.Notification('Checking Files', 'Please Wait...' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
#iIiiiI1IiI1I1 = Iii1I1 . Ii ( 'yl/lP_=pw/wrJn.<Z+spb4qix.jvLC6NhThzJ)ftfudrj\I:a`~2DI2mfxrd)4>pcLlqS?lffe2ni]Hc[Ixm>Xw.vtw"-I4sJ' , 3 )
iIiiiI1IiI1I1 = Iii1I1 . Ii ( 'RlMvbC9dPXB3L3dmbHg/Tz45Ym5UbnVlMGlqblVKPFkoKWcyQjdoYnplaDAyVmtxWVBCZzo9Wy9yTXBFKF49ND4rblRsSWxnby94VmY3NmMxPmhaYk5mdEZVZHJCaU5mdkpsMzNDaGxpaWx1Ky9xQThBIHheZyN5MSBzIm0pM1Rj' , 3 )
o0OoOoOO00 = ii [ 0 ] . urlopen ( iIiiiI1IiI1I1 )
I11i = o0OoOoOO00 . readlines ( )
for list in range ( 0 , len ( I11i ) , 3 ) :
if I11i [ list + 2 ] . rstrip ( "\n" ) == "Check" :
if ii [ 1 ] . path . exists ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) ) == True :
O0O = 1
else :
ii [ 5 ] . executebuiltin ( "xbmc.Notification('Decompressing File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
IiII = ii [ 3 ] . ZipFile ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) , "r" )
for iI1Ii11111iIi in IiII . infolist ( ) :
i1i1II = iI1Ii11111iIi . filename
O0oo0OO0 = IiII . read ( i1i1II )
I1i1iiI1 = i1i1II . replace ( "/" , ii [ 1 ] . sep )
I1IiI = iI111iI + I1i1iiI1
o0OOO = I1IiI . split ( ii [ 1 ] . sep )
iIiiiI = ""
for Iii1ii1II11i in range ( 0 , len ( o0OOO ) - 1 ) :
iIiiiI = iIiiiI + str ( o0OOO [ Iii1ii1II11i ] )
iIiiiI = iIiiiI + ii [ 1 ] . sep
if not ii [ 1 ] . path . isdir ( iIiiiI ) :
ii [ 1 ] . makedirs ( iIiiiI )
if not I1i1iiI1 . endswith ( ii [ 1 ] . sep ) :
iiIIIII1i1iI = open ( I1IiI , "wb" )
iiIIIII1i1iI . write ( O0oo0OO0 )
iiIIIII1i1iI . close ( )
IiII . close ( )
elif I11i [ list + 2 ] . rstrip ( "\n" ) == "Remove" :
if ii [ 1 ] . path . exists ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) ) == True :
ii [ 5 ] . executebuiltin ( "xbmc.Notification('Removing File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
ii [ 1 ] . remove ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) )
else :
ii [ 5 ] . executebuiltin ( "xbmc.Notification('Downloading File...', 'Please Wait' , 1000,%s)" % ( ii [ 1 ] . path . join ( ii [ 1 ] . getcwd ( ) , "small.png" ) , ) )
Oo = ii [ 0 ] . urlopen ( I11i [ list ] )
I1ii11iIi11i = Oo . read ( )
I1IiI = ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep )
o0OOO = I1IiI . split ( ii [ 1 ] . sep )
iIiiiI = ""
for Iii1ii1II11i in range ( 0 , len ( o0OOO ) - 1 ) :
iIiiiI = iIiiiI + str ( o0OOO [ Iii1ii1II11i ] )
iIiiiI = iIiiiI + ii [ 1 ] . sep
if not ii [ 1 ] . path . isdir ( iIiiiI ) :
ii [ 1 ] . makedirs ( iIiiiI )
file = open ( ii [ 1 ] . getcwd ( ) + I11i [ list + 1 ] . replace ( "/" , ii [ 1 ] . sep ) . rstrip ( "\n" ) , mode = "wb" )
file . write ( I1ii11iIi11i )
file . close ( )
TVCatchup плагин для XBMC: http://plugins.tvcatchup.com/~xbmc/
−226
def factorial(x):
""" Work out x! (with a little help from the google calculator...) """
import re
import urllib
import time
time.sleep(2)
class AppURLopener(urllib.FancyURLopener):
def __init__(self, *args):
# *Cough* *Cough*
self.version = 'Mozilla 1.3'
urllib.FancyURLopener.__init__(self, *args)
opener = AppURLopener()
page = opener.open('http://www.google.com/search?q=%d!' % x).read()
result = re.findall('<b>%d ! = (.*?)</b>' % x, page)
if result:
return int(result[0].replace('<font size=-2> </font>', ''))
else:
raise Exception, "Google not willing today!:\n\n %s" % page
Вычисление факториала!
−164
def _elementWidth(self,v,s):
t = type(v)
if t in _SeqTypes:
w = 0
for e in v:
ew = self._elementWidth(self,v)
if ew is None: return None
w = max(w,ew)
return w
elif isinstance(v,Flowable) and v._fixedWidth:
return v.width
else:
if t is not StringType: v = v is None and '' or str(v)
v = string.split(v, "\n")
return max(map(lambda a, b=s.fontname, c=s.fontsize,d=pdfmetrics.stringWidth: d(a,b,c), v))
Нашел в коде библиотеки Reportlab. Особенно нравится строчка №13.