노출되는 이미지가 불편하시겠지만 양해를 구합니다. 노출, 클릭등에 관한 자료로 활용 중입니다.

Python Lists , 리스트


원문에서 제공하는 예제는 실제 실행시에, 예약어등의 사용으로 warning과 error가 발생하므로 수정해서 실행을 해야 한다. 이 실행 결과를 정리한다.



##

## Python Lists , 리스트 기본

##


# Lists work similarly to strings - use the len() function and square brackets[] 

# to access data, with the first element at index 0

colors = ['blue', 'blue', 'green']


print (colors[0]) ## blue

print (colors[2]) ## green

print (len(colors)) ## 3


# Assignment with an = on lists doest not make a copy. 복사 금지

# Instead, assignment makes the two variables point to the one list in memory

b = colors ## Does not copy the list


print(b) ## ['blue', 'blue', 'green']


empty_list = [] ## is just an empty pair of brackets []


yield_list = [1,2] + [3, 4] ## The '+' works to append two lists

print(yield_list) ## [1, 2, 3, 4]



##

## FOR and IN , 생성자 For

##


# Python's *for* and *in* constructs are extremely useful, 

# and the first use of them we'll see is with lists.

squares = [1, 4, 9, 16]

total_value = 0

# The *for* construct - for var in list 

#    is an easy way to look at each element in a list.

#    Do not add or remove from the list during iteration.

for num in squares:

  total_value += num

print( total_value ) ##30


# The *in* construct on ite own is an easy way to test if an element appears

# in a list(or other collection) - value in collection - tests if the value is

# in the collection, returning True/False

name_list = ['larry', 'curly', 'moe']

if 'curly' in name_list:

  print('yay')


# test_strings = 'abcd'

# for ch in test_strings:

#   print(ch) ## a b c d


##

## Range , 범위 ( 0 ~ n-1 ) 

##


# The range(n) function yields the numbers 0,1, ... n-1 and range(a,b) returns

# a, a+1, ... b-1 up to but not including the last number.

# for i in range(100):

#   print(i)


##

## While Loop, 반복 While

##


# Python also has the standard while-loop, and the *break* and *continue* statements work

# as in C++ and Java, altering the course of the innermost loop.

i = 1

a = 'abc'

while i < len(a):

  print (a[i]) ## b

  i = i + 3



##

## List Methods , 함수들

##


# Here are some other common list methods.

# list.append(elem) : adds a single element to the end of the list

# list.insert(index, elem) : inserts the element at the given index

# list.extend(list2) : adds the elements in list2 to the end of the list

# list.index(elem) : searches for the given element from the start of the list and returns its index

# list.remove(elem) : searches for the first instance of the given element and removes it

# list.sort() : sorts the list in place(does not return it)

# list.reverse() : reverses the list in place(does not return it)

# list.pop(index) : removes and returns the element at the given index

str_list = ['larry', 'curly', 'moe']

str_list.append('shemp')

str_list.insert(0,'xxx')

str_list.extend(['yyy', 'zzz'])

print(str_list) ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']

print(str_list.index('curly')) ## 2


str_list.pop(1)

print(str_list) ## ['xxx', 'curly', 'moe', 'shemp', 'yyy', 'zzz']


# common error 

# : note that the above methods do not *return* the modified list

#   they just modify the original list

num_list = [1, 2, 3]

print(num_list.append(4)) ## None

num_list.append(4)

print(num_list) ## [1, 2, 3, 4, 4]


##

## List Build up, 생성하기

##


# One common pattern is to start a list a the empty list[],

# then use append() or extend() to add elements to it

empty_list = []

empty_list.append('a')

empty_list.append('b')

print(empty_list) ## ['a', 'b']


##

## List Slices, 자르기 [start:end:step]

##


# Slices work on lists just as with strings, and can also be used to change

# sub-parts of the list


char_list = ['a', 'b', 'c', 'd']

print(char_list[1:-1]) ## ['b', 'c']

print(char_list[:]) ## ['a', 'b', 'c', 'd']

print(char_list[1:2]) ## ['b']

char_list[0:2] = 'z'

print(char_list) # ['z', 'c', 'd']



원본 

https://developers.google.com/edu/python/lists















블로그 이미지

StartGuide

I want to share the basic to programming of each category and how to solve the error. This basic instruction can be extended further. And I have been worked in southeast Asia more than 3 years. And I want to have the chance to work another country.

,