Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Reading
#6
In that form is just to create a list, as the [x for x in xxx] is a syntactic sugar for list(x for x in xxx).
In particular the part of "x for x in xxx" produces a generator, that you can pass it to any place in python that accepts a generator as input (behaves as the command "range")

The other 2 "pretty" syntax similar to that one is to create a set or a dictionary:
# Imagine fd contains 1, 2, 555, 44
# You only want the different numbers, no matter the repetitions => Create set of numbers
>>> {int(s.strip()) for s in fd}
{1, 2, 555, 44}

# You want to create a dictionary initialising the entry to something
>>> {int(s.strip()): 'xxx' for s in fd}
{1: 'xxx', 2: 'xxx', 44: 'xxx', 555: 'xxx'}

# If you want to create a tuple you can do it with:
>>> tuple(int(s.strip()) for s in fd)
(1, 2, 44, 555)
Reply


Messages In This Thread
File Reading - by toxicxarrow - May-07-2018, 09:40 AM
RE: File Reading - by ThiefOfTime - May-07-2018, 10:14 AM
RE: File Reading - by wavic - May-07-2018, 10:30 AM
RE: File Reading - by killerrex - May-07-2018, 10:35 AM
RE: File Reading - by toxicxarrow - May-07-2018, 10:57 AM
RE: File Reading - by killerrex - May-07-2018, 01:47 PM
RE: File Reading - by wavic - May-07-2018, 02:02 PM
RE: File Reading - by toxicxarrow - May-07-2018, 03:02 PM
RE: File Reading - by wavic - May-07-2018, 03:08 PM
RE: File Reading - by toxicxarrow - May-07-2018, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,091 Mar-31-2021, 11:53 AM
Last Post: snippsat
  reading from a file looseCannon101 14 5,203 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Weird problem with reading from file and performing calculations pineapple999 1 3,070 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 5,034 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,941 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,569 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Reading of structured .mat (matlab) file sumit 2 3,498 May-24-2018, 12:12 PM
Last Post: sumit
  reading all lines from a text file seadoofanatic 2 2,979 Mar-13-2018, 06:05 PM
Last Post: Narsimhachary
  Reading a text file fivestar 7 5,733 Oct-13-2017, 07:25 AM
Last Post: gruntfutuk
  Dictionary + File Reading palmtrees 2 4,955 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020