Giter Site home page Giter Site logo

python-study-notes's Introduction

python-study-notes

reference to do basic operations on iterable objects

get substring add item remove item filter list map list
split by delimiter join with delimiter reverse list sort list get max
get min is item in list is instance of type get index of item in list get indices of item in list
iterate by index get column in 2d matrix uppercase string lowercase string get unique list items

get substring

letters = "abcdef"
r = letters[0]
print(r)

>> "a"
letters = "abcdef"
r = letters[:-1]
print(r)

>> "abcde"
letters = "abcdef"
r = letters[1:]
print(r)

>> "bcdef"
letters = "abcdef"
r = letters[1:-1]
print(r)

>> "bcde"

add item

items = [1,2,3,4,5]
items.append(6)
print(items)

>> [1, 2, 3, 4, 5, 6]
items = [1,2,3,4,5]
items.insert(2, 'r')
print(items)

>> [1, 2, 'r', 3, 4, 5]

remove item

items = [1,2,3,4,5]
del items[0:2]
print(items)

>> [3, 4, 5]
items = [1,2,3,4,5]
items.pop(2)
print(items)

>> [1, 2, 4, 5]

filter list

items = [1,2,3,4,5,6,7,8]
r = [i for i in items if i % 2 == 0]
print(r)

>> [2, 4, 6, 8]
items = [1,2,3,4,5,6,7,8]
r = list(filter(lambda i: i % 2 == 0, items))
print(r)

>> [2, 4, 6, 8]

map list

items = [1,2,3,4,5]
r = [i * 2 for i in items]
print(r)

>> [2, 4, 6, 8, 10]
items = [1,2,3,4,5]
r = list(map(lambda i: i * 2, items))
print(r)

>> [2, 4, 6, 8, 10]

split by delimiter

letters = 'a b c d e f'
r = letters.split(' ')
print(r)

>> ['a', 'b', 'c', 'd', 'e', 'f']

join with delimiter

items = ['a','b','c','d','e']
r = " ".join(items)
print(r)

>> "a b c d e"

reverse list

items = [1,2,3,4,5]
r = items[::-1]
print(r)

>> [5, 4, 3, 2, 1]
items = [1,2,3,4,5]
items.reverse()
print(items)

>> [5, 4, 3, 2, 1]

sort list

items = [2,4,6,1,2,3]
r = sorted(items)
print(r)

>> [1, 2, 2, 3, 4, 6]
items = [2,4,6,1,2,3]
r = sorted(items, reverse=True)
print(r)

>> [6, 4, 3, 2, 2, 1]

get max

items = [1,2,3,6,5,4]
r = max(items)
print(r)

>> 6

get min

items = [6,5,4,1,2,3]
r = min(items)
print(r)

>> 1

is item in list

items = ['a','b','c','d','e']
print(True) if ('d' in items) else print(False)

>> True

is instance of type

isinstance([1,2,3], list)

>> True
isinstance({'a': 1, 'b': 2}, dict)

>> True

get index of item in list

items = ['a','b','c','d','e']
r = items.index('c')
print(r)

>> 2

get indices of item in list

items = [1,2,3,2,4,6,3,6,9]
r = [i for i in range(len(items)) if items[i] == 3]
print(r)

>> [2, 6]

iterate by index

items = ['a','b','c','d','e']
for i in range(len(items)):
  print(i, end=' ')

>> 0 1 2 3 4

get column in 2d matrix

items = [
  [1,2],
  [3,4],
  [5,6],
  ]
r = [i[0] for i in items]
print(r)

>> [1, 3, 5]

uppercase string

letters = "abcde"
r = letters.upper()
print(r)

>> 'ABCDE'

lowercase string

letters = "ABCDE"
r = letters.lower()
print(r)

>> 'abcde'

get unique list items

items = [1,2,2,3,3,3]
r = []
for i in items: i not in r and r.append(i)
print(r)

>> [1, 2, 3]

python-study-notes's People

Contributors

senaonp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.