Skip to content

Instantly share code, notes, and snippets.

@huseyin
Last active October 25, 2015 19:17
Show Gist options
  • Save huseyin/3d77946c9f21d9127f2c to your computer and use it in GitHub Desktop.
Save huseyin/3d77946c9f21d9127f2c to your computer and use it in GitHub Desktop.
Python list comprehension
# -*- coding: utf-8 -*-
# Cümle içerisindeki kelimelerin uzunluklarını döndür
def wordsize(string):
""" wordsize(string)
"""
return [len(word) for word in string.split()]
# Ufak bir test aracı
def unittest(str, expr):
""" unittest(str, expr->expression)
"""
if wordsize(str) == expr:
return True
else:
return False
if __name__ == '__main__':
str = "foo bar baz"
print unittest(str, [3, 3, 3])
print unittest(str, [2, 4, 1])
@huseyin
Copy link
Author

huseyin commented Oct 25, 2015

Şöyle de olabilir miydi ?

def wordsize(string):
    """ wordsize(string)
    """
    return map(lambda word: len(word), string.split())

Neden olmasın ki? Olur olur. lambda daha şık olur hem de 😊 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment