Sponsored Content
Full Discussion: 2 lines of python
Top Forums Shell Programming and Scripting 2 lines of python Post 303002424 by durden_tyler on Thursday 24th of August 2017 02:14:16 AM
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:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

**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

7. 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

8. 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

9. 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
QTextDecoder(3qt)														 QTextDecoder(3qt)

NAME
QTextDecoder - State-based decoder SYNOPSIS
All the functions in this class are reentrant when Qt is built with thread support.</p> #include <qtextcodec.h> Public Members virtual ~QTextDecoder () virtual QString toUnicode ( const char * chars, int len ) = 0 DESCRIPTION
The QTextDecoder class provides a state-based decoder. The decoder converts a text format into Unicode, remembering any state that is required between calls. See also QTextCodec::makeEncoder() and Internationalization with Qt. MEMBER FUNCTION DOCUMENTATION
QTextDecoder::~QTextDecoder () [virtual] Destroys the decoder. QString QTextDecoder::toUnicode ( const char * chars, int len ) [pure virtual] Converts the first len bytes in chars to Unicode, returning the result. If not all characters are used (e.g. if only part of a multi-byte encoding is at the end of the characters), the decoder remembers enough state to continue with the next call to this function. SEE ALSO
http://doc.trolltech.com/qtextdecoder.html http://www.trolltech.com/faq/tech.html COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. AUTHOR
Generated automatically from the source code. BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qtextdecoder.3qt) and the Qt version (3.1.1). Trolltech AS 9 December 2002 QTextDecoder(3qt)
All times are GMT -4. The time now is 09:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy