Forum: Poser Python Scripting


Subject: Useful Code Snippets

structure opened this issue on Jun 27, 2019 ยท 94 posts


structure posted Fri, 11 March 2022 at 1:56 AM Forum Coordinator

example usage of casefold, Counter and count

# setup main tuple
items=("Extra-Terrestrial", "Alien", "Planet", "Star",
        "Galaxy", "Universe", "Black Hole", "White Hole",
        "Astronomy", "Dark-Matter", "Dark-Energy")

# setup comparison tuple
compare=("extra-terrestrial", "alien")

''' use casefold -py3(or upper / lower -py2)
for context insensitive comparison '''
for item in items:
    if any(i.casefold() in item.casefold() for i in compare):
        print(item)

''' use counter to count all words in tuple'''
print(f'\n{Counter(compare)}\n')

''' use count to count how many times comparative
    words appear in tuple'''
for c in compare:
    tense="time" if items.count(c.title())==1 else "times"
    print(f'{c.title()} appears {items.count(c.title())}

Output:

Extra-Terrestrial Alien

Counter({'extra-terrestrial': 1, 'alien': 1}) Extra-Terrestrial appears 1 time in ('Extra-Terrestrial', 'Alien', 'Planet', 'Star', 'Galaxy', 'Universe', 'Black Hole', 'White Hole', 'Astronomy', 'Dark-Matter', 'Dark-Energy', '') Alien appears 1 time in ('Extra-Terrestrial', 'Alien', 'Planet', 'Star', 'Galaxy', 'Universe', 'Black Hole', 'White Hole', 'Astronomy', 'Dark-Matter', 'Dark-Energy', '')

**note :
.count only takes into account the exact word - so words like
"alienesque" or "alien ship" would not be recognised as containing
the word alien.

Locked Out