User Tools

Site Tools


python_lists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python_lists [2023/05/01 15:50] – [read file as list of strings] adminpython_lists [2023/11/07 21:51] (current) – [search one list in another] admin
Line 1: Line 1:
 ===== dummy ===== ===== dummy =====
 ==== tasks ==== ==== tasks ====
 +  * [[check if two lists are equal]]
   * [[get unique items in a list]]   * [[get unique items in a list]]
   * [[convert a string to a list]]   * [[convert a string to a list]]
 +
 +==== search one list in another ====
 +<code>
 +>>> letters = [i for i in 'abcdefghijklmnopqrstuvwxyz']
 +>>> letters
 +['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
 +>>> name = [i for i in 'kamaraju']
 +>>> name
 +['k', 'a', 'm', 'a', 'r', 'a', 'j', 'u']
 +>>> indices = [letters.index(i) for i in name]
 +>>> indices
 +[10, 0, 12, 0, 17, 0, 9, 20]
 +>>> rebuilt_name = [letters[i] for i in indices]
 +>>> rebuilt_name
 +['k', 'a', 'm', 'a', 'r', 'a', 'j', 'u']
 +</code>
 +
 +tags | identify the indices
 +
  
 ==== read file as list of strings ==== ==== read file as list of strings ====
Line 11: Line 31:
 </code> </code>
  
 +==== Check if a variable is a list ====
 +<code>
 +% python
 +Python 3.11.4 (main, Jul  5 2023, 13:45:01) [GCC 11.2.0] on linux
 +Type "help", "copyright", "credits" or "license" for more information.
 +>>> isinstance(['foo'], list)
 +True
 +>>> isinstance('foo', list)
 +False
 +</code>
  
python_lists.1682956257.txt.gz · Last modified: 2023/05/01 15:50 by admin