Saori Yoshimoto work notes since 2018

Showing posts with label Scripts/Python. Show all posts
Showing posts with label Scripts/Python. Show all posts

Monday, November 6, 2023

[Python] Basic syntax

  • range()
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(5,10)
[5, 6, 7, 8, 9]
>>> list(range(0,10,3))
[0, 3, 6, 9]
>>> list(range(10,0,-3))
[10, 7, 4, 1]

range(stop)                0 ≦ i < stop
range(start, stop)          start ≦ i < stop
range(start, stop, step)  start ≦ i < stop
range(stop, start, -step)  stop ≧ i > start

  • for()
>>> names = ['pico', 'ted', 'hop']
>>> for i in names:
...      print i
pico
ted
hop
  • remove(), del()
>>> names = ['pico', 'ted', 'hop']
>>> names.remove('pico')
>>> names
['ted', 'hop']
>>> names.pop(1)
'hop'
>>> names
['ted']
  • random(), uniform(), randrange(), randint()
>>> num = range(5)
>>> num
[0, 1, 2, 3, 4]
>>> random.randint(0, len(nums)-1)
4

Wednesday, April 29, 2020

[Python] random function

import random

names = ['apple', 'orenge', 'banana', 'pineapple', 'lemon']

random_number = random.randint(0,len(names)-1)
chosen_one    = names[random_number]
print(chosen_one)

Wednesday, March 11, 2020

[Python] rename script in Linux

import glob, os, re

first_num = 1

files = sorted(glob.glob("*.exr"))
num = lambda files : int(re.sub("\\D", "", files))

files_sort = sorted(files, key=num)
#print(files_sort)

for i, old_name in enumerate(files_sort):
old_name_lib = old_name.split('.')
new_name1 = old_name_lib[0] + "." + old_name_lib[1] + "."
new_name2 = "{0:04d}.exr".format(i + first_num)

    os.rename(old_name,  new_name1 + new_name2)
    print(old_name + " = " + new_name1 + new_name2)

Saturday, February 16, 2019

[Python] for loop

# Prints out the numbers 2,3,5,7
primes = [2, 3, 5, 7]
for prime in primes:
    print(prime)

# Prints out the numbers 0,1,2,3,4
for x in range(5):
    print(x)

# Prints out 3,4,5
for x in range(3, 6):
    print(x)

# Prints out 3,5,7
for x in range(3, 8, 2):
    print(x)

# Prints out 0,1,2,3,4
count = 0
while count < 5:
    print(count)
    count += 1  # This is the same as count = count + 1

[Python] character string

-split function
https://www.pythonforbeginners.com/dictionary/python-split

-convert  int to strings
str(value)