[python] Create a list of various character types

Inspired by ~~ Make a simple list of alphabets with Python ~~ (broken link), make a list of various strings saw.

I think it can be used in password dictionaries and wordplay games.

Postscript (2018/03/30)

It seems that some people are watching it once in a while, so I will explain the module in the comment section as well. I think it is best practice to use the string module for half-width character strings.

>>> import string
>>> help(string)
(Omission)
DATA
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv...\xaf\xb0...
    lowercase = 'abcdefghijklmnopqrstuvwxyz'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    whitespace = '\t\n\x0b\x0c\r '
>>> string.digits
'0123456789'

In Python, the handling of lists and strings is not so different, so I will omit the explanation of that part.

Lowercase alphabet

[chr(i) for i in range(97, 97+26)]
# [chr(i) for i in range(ord('a'), ord('z')+1)]

Uppercase alphabet

[chr(i) for i in range(65, 65+26)]
# [chr(i) for i in range(ord('A'), ord('Z')+1)]

Half-width numbers

[chr(i) for i in range(48, 48+10)]
# [chr(i) for i in range(ord('0'), ord('9')+1)]

Hiragana

[chr(i) for i in range(12353, 12436)]
# [chr(i) for i in range(ord('Ah'), ord('Hmm')+1)]

Katakana

[chr(i) for i in range(12449, 12532+1)]
# [chr(i) for i in range(ord('A'), ord('Down')+2)]
#If you don't need "Vu",-1 "please

Full-width numbers

[chr(i) for i in range(65296, 65296+10)]
# [chr(i) for i in range(ord('0'), ord('9')+1)]

Common kanji

that is impossible. Continuously. However, there is nothing you can't do.

[UTF-8 version of common kanji code table] I think there is no choice but to extract the kanji from csv and list them ...

After deleting the comment field, execute the following.

import csv
kanji = []
with open('/path/to/joyo-kanji-code-u.csv', 'r') as f:
    data = csv.reader(f)
    
    for row in data:
        kanji.append(row[0])

Yup. It looks like a list of 2136 pieces ... Please do your best to make things that include the outside.

Half-width symbol

This is also impossible continuously. Rare gas that the person who set ASCII is bad ... I didn't come up with a good method, so I'll give an example.

eisu = [chr(i) for i in range(97, 97+26)]
eisu.extend([chr(i) for i in range(65, 65+26)])
eisu.extend([chr(i) for i in range(48, 48+10)])

[chr(i) for i in range(33, 127) if chr(i) not in eisu]
#By the way, if you change "33" to "32", a half-width space will be inserted.

As you may have noticed, for all half-width characters, [chr (i) for i in range (32, 127)]

bonus

I think it's a hassle to copy and paste one by one, so I'll put a function that returns the list I want. Of course, please add common kanji and half-width symbols afterwards.

#Lowercase alphabet →(97, 123)
#Uppercase alphabet →(65, 91)
#Half-width numbers →(48, 58)
#Hiragana →(12353, 12436)
#Katakana →(12449, 12532+1)
#Full-width numbers →(65296, 65306)

def moji_list(*args):
    moji = []
    for i in range(len(args)):
        moji.extend([chr(j) for j in range(args[i][0], args[i][1])])
    return moji

moji_list((97, 123), (65, 91), (48, 58))
# ['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', '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

I think it is also possible to specify it with ord ().

Recommended Posts

[python] Create a list of various character types
[Python] List Comprehension Various ways to create a list
[Python] How to make a list of character strings character by character
Python: Create a dictionary from a list of keys and values
Display a list of alphabets in Python 3
[python] Get a list of instance variables
[Python] Get a list of folders only
I tried to create a list of prime numbers with python
List of python modules
Create a Python module
Make a copy of the list in Python
Create a Python environment
Various processing of Python
[Python] Create a list of date and time (datetime type) for a certain period
How to write a list / dictionary type of Python3
Python: Get a list of methods for an object
Group by consecutive elements of a list in Python
Create a Wox plugin (Python)
Create a function in Python
Summary of Python3 list operations
Create a dictionary in Python
Create ToDo List [Python Django]
About various encodings of Python 3
Python list is not a list
2.x, 3.x character code of python
Create a python numpy array
[Python] Copy of multidimensional list
Create a directory with python
Create a list in Python with all followers on twitter
How to shuffle a part of a Python list (at random.shuffle)
[Python] Create a date and time list for a specified period
Get the number of specific elements in a python list
Get a list of purchased DMM eBooks with Python + Selenium
Since Python 1.5 of Discord, I can't get a list of members
How to get a list of built-in exceptions in python
Create a pixel art of Levi Captain with Python programming!
Spit out a list of file name, last modified date and character code in python3
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
A good description of Python decorators
[python] Manage functions in a list
Create a python GUI using tkinter
Create a DI Container in Python
[Python] A memorandum of beautiful soup4
Create a Python environment on Mac (2017/4)
A brief summary of Python collections
Create a virtual environment with Python!
Create a binary file in Python
python / Make a dict from a list.
Create a python environment on centos
Create a Python general-purpose decorator framework
Create a Kubernetes Operator in Python
5 Ways to Create a Python Chatbot
Create a random string in Python
Generate a list of consecutive characters
About the basics list of Python basics
Create an instance of a predefined class from a string in Python
Try to get a list of breaking news threads in Python.
Dig the directory and create a list of directory paths + file names
Various ways to create an array of numbers from 1 to 10 in Python.
Get a list of files in a folder with python without a path
[Python] A program that rotates the contents of the list to the left