Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • damian6: hello i wanne buy this server can u add me
      October 20, 2017, 03:54:38 PM
    • Wilkooo: lol jks
      October 20, 2017, 02:39:53 PM
    • Wilkooo: ilyall
      October 20, 2017, 02:39:48 PM
    • tm4dz: why cant i see any download links??
      October 20, 2017, 10:46:55 AM
    • stCky: u dont need to its fudgeing sick
      October 19, 2017, 08:13:13 PM
    • odin stormborn: how do i change my username
      October 19, 2017, 12:16:51 PM
    • qazplm102938: everyone play manicps its best server out there loads to do its badboyyyy - manicps manicps shout out kiteshield
      October 19, 2017, 10:28:21 AM
    • qazplm102938: everyone play manicps its best server out there loads to do its badboyyyy - manicps manicps
      October 19, 2017, 10:11:08 AM
    • Travas: wtf
      October 19, 2017, 07:37:52 AM
    • stCky: Adaro, you are a threat to the communities image, yes
      October 18, 2017, 02:53:43 PM
    • Krackkid: Hello everyone it is I, Krackkid.
      October 18, 2017, 07:21:56 AM
    • soma_: Does anyone have an updated link to a blank Source / Client? 317/508
      October 18, 2017, 04:57:44 AM
    • michaeltrailer: olm t pst
      October 18, 2017, 04:52:48 AM
    • Adaro: Strange that staff think I'm a threat when I'm not threatening anyone
      October 18, 2017, 03:07:16 AM
    • Adaro: My Skype is: Mod Adaro
      October 18, 2017, 02:47:02 AM
    • Adaro: Make sure to PM me if you need help on Moparscape. I can answer any questions.
      October 18, 2017, 02:46:48 AM
    • Adaro: Wilko unblock me on discord
      October 18, 2017, 02:45:07 AM
    • Striker Fox2: Make sure to check out the new price guide - [link]
      October 18, 2017, 12:45:49 AM
    • stCky: yes, drhouse.cum
      October 17, 2017, 10:29:20 PM
    • Travas: does anyone know a good site for penis enlargement pills?
      October 17, 2017, 09:19:51 PM

    Author Topic: [Tutorial] Inline for loops in Python  (Read 607 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineAmbokile

    • Member
    • ****
    • Posts: 3,009
    • Thanks: +0/-0
      • View Profile
    [Tutorial] Inline for loops in Python
    « on: June 06, 2015, 08:33:31 AM »


    Quote
    Python is a language that has the potential to make pieces of code very short. There are lots of tricks to turn lengthy code operations that would require multiple lines in other languages into a neat, single line snippet.

    While I was working on an upcoming tutorial on web scraping, I had an array of HTML tags and I wanted to get an array containing only the text of the tags. My first solution was a bulky one but I managed to quickly turn it into something more Python-esque.



    Looking for feedback as usual.


    OfflineBowser jr

    • Member
    • ****
    • Posts: 6,001
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #1 on: June 06, 2015, 11:23:59 AM »
    Stop bashing other languages for the need of writing lots of code/lines to achieve something similar.
    Code: Scala
    1. print(0 to 9 mkString " - ")
    A lot shorter in scala (21 chars shorter.) So don't think Python is unique in that aspect, and I'm sure Java 8 can produce something similarly short.

    To criticize your content, it seems incomplete. To be of any value you should include more information about for loops, iteration and lambda functions.
    « Last Edit: June 06, 2015, 11:46:41 AM by Bowser jr »

    OfflineAmbokile

    • Member
    • ****
    • Posts: 3,009
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #2 on: June 06, 2015, 11:48:34 AM »
    Stop bashing other languages for the need of writing lots of code/lines to achieve something similar.
    Code: Scala
    1. print(0 to 9 mkString " - ")
    A lot shorter in scala (21 chars shorter.) So don't think Python is unique in that aspect, and I'm sure Java 8 can produce something similarly short.

    To criticize your content, it seems incomplete. To be of any value you should include more information about for loops, iteration and lambda functions.

    I wasn't bashing other languages! I just think that as a whole Python can produce shorter code than the other languages I'm used to (C#, PHP, Java)


    Offlinejustaguy

    • Member
    • ****
    • *
    • Posts: 707
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #3 on: June 06, 2015, 01:16:41 PM »


    Quote
    Python is a language that has the potential to make pieces of code very short. There are lots of tricks to turn lengthy code operations that would require multiple lines in other languages into a neat, single line snippet.

    While I was working on an upcoming tutorial on web scraping, I had an array of HTML tags and I wanted to get an array containing only the text of the tags. My first solution was a bulky one but I managed to quickly turn it into something more Python-esque.



    Looking for feedback as usual.

    The feature you are describing in this article are called array/object comprehensions. They are coming to PHP 7 and ECMAScript 6 (new version of JavaScript).
    RIP

    OfflineHcoJustin

    • Member
    • ****
    • Posts: 2,400
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #4 on: June 06, 2015, 01:25:02 PM »
    Code: Ruby
    1. titles.collect! {|title| title.string}


    Offlinet4

    • Member
    • ****
    • *
    • *
    • *
    • Posts: 6,798
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #5 on: June 06, 2015, 01:57:39 PM »
    there's a difference between inline and one line.

    OfflineWhackatre

    • u wan buy dvd?
    • Member
    • ****
    • *
    • Posts: 1,780
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #6 on: June 06, 2015, 05:42:15 PM »
    am i the only one who strongly prefers using map (yay) to list comprehensions (eww)?

    Code: Python
    1. print(' - '.join(map(str,range(0,10))))
    [20:14:10] <dicklord> your to smart :'( You make botserv bots say something you enjoy and want to go to the boards.

    If you're looking for a supreme text editor for everything look no further than Emacs.

    OfflineBowser jr

    • Member
    • ****
    • Posts: 6,001
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #7 on: June 06, 2015, 05:47:03 PM »
    I don't see why you would want to use map here.

    OfflineWhackatre

    • u wan buy dvd?
    • Member
    • ****
    • *
    • Posts: 1,780
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #8 on: June 06, 2015, 05:52:36 PM »
    I don't see why you would want to use map here.

    you would use map to convert the list of ints into a list of strs to use join.
    [20:14:10] <dicklord> your to smart :'( You make botserv bots say something you enjoy and want to go to the boards.

    If you're looking for a supreme text editor for everything look no further than Emacs.

    Offlineargv

    • First Time Poster
    • *
    • Posts: 1
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #9 on: June 09, 2015, 07:21:03 PM »
    Seems much neater:
    Code: Perl
    1. $_=$_->stringfor@titles;
    2. printjoin" - ",0..10;
    « Last Edit: June 09, 2015, 07:43:04 PM by argv »

    Offlinejustaguy

    • Member
    • ****
    • *
    • Posts: 707
    • Thanks: +0/-0
      • View Profile
    Re: [Tutorial] Inline for loops in Python
    « Reply #10 on: June 09, 2015, 08:08:25 PM »
    Seems much neater:
    Code: Perl
    1. $_=$_->stringfor@titles;
    2. printjoin" - ",0..10;

    Wow... It's been a while since I've seen anyone use Perl.
    RIP

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal