2 lines of python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 lines of python
# 1  
Old 08-24-2017
2 lines of python

Hi,

Please can you tell me what the last two lines in the python code below are doing?

Code:
data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print 'data has %d characters, %d unique.' % (data_size, vocab_size)
char_to_ix = { ch:i for i,ch in enumerate(chars) }
ix_to_char = { i:ch for i,ch in enumerate(chars) }

# 2  
Old 08-24-2017
Quote:
Originally Posted by 1Brajesh
...
Please can you tell me what the last two lines in the python code below are doing?
Code:
data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print 'data has %d characters, %d unique.' % (data_size, vocab_size)
char_to_ix = { ch:i for i,ch in enumerate(chars) }
ix_to_char = { i:ch for i,ch in enumerate(chars) }

This line:
Code:
char_to_ix = { ch:i for i,ch in enumerate(chars) }

creates a Dictionary called "char_to_ix" that has for its keys, each character from the list "chars". The corresponding values are numbers from 0 to len(chars)-1.

This line:

Code:
ix_to_char = { i:ch for i,ch in enumerate(chars) }

creates a Dictionary called "ix_to_char" that has the reverse key/value pairs as those of "char_to_ix". So each key of "ix_to_char" is a number from 0 to len(chars)-1 and its value is a character from the list "chars".
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-24-2017
Further to the above post, the last two lines use a python feature called list comprehension. I suggest you look that up in the Python documentation.

Andrew
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Python: Suppress lines not substituted

Hi all, I started playing around with Python (2.6.6) trying to parse a file. No matter what I tried so far I am not able to print only the lines/group/backreference that have been affected by the substitution. Instead I get also the other lines too File to parse: some more text ... (2 Replies)
Discussion started by: zaxxon
2 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

8. Programming

Python: bash-shell-like less functionality in the python shell

Hello, Is there some type of functional way to read things in the Python shell interpreter similar to less or more in the bash (and other) command line shells? Example: >>> import subprocess >>> help(subprocess) ... ... I'm hoping so as I hate scrolling and love how less works with... (0 Replies)
Discussion started by: Narnie
0 Replies

9. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question