close

 cities = ["台南","雲林","高雄","屏東"]

  for i in range(len(cities)):
print(str(i)+""+cities[i])

0台南
1雲林
2高雄
3屏東

----------------------------------------------------

range returns an object that produces a sequence of integers from start (inclusive)

to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.

----------------------------------------------------

for i in range(len(cities)):
print(f"{i}.{cities[i]}")

0.台南
1.雲林
2.高雄
3.屏東

----------------------------------------------------

# i:表示索引 city:索引值
for i, city in enumerate(cities):
print(f"{i}.{city}")

----------------------------------------------------

enumerate is useful for obtaining an indexed list:

(0, seq[0]), (1, seq[1]), (2, seq[2]), ...

----------------------------------------------------

cities = ["台南","雲林","高雄","屏東"]

population = [1874917,676873,2765932,812658]

for city, pop in zip(cities, population):
print(f"{city}有{pop}人")

 

台南有1874917人
雲林有676873人
高雄有2765932人
屏東有812658人

----------------------------------------------------

zip(*iterables) --> A zip object yielding tuples until input is exhausted.

----------------------------------------------------

cities = ["台南","雲林","高雄","屏東"]

population = [1874917,676873,2765932,812658]

list(zip(cities, population))

 

Out[5]: [('台南', 1874917), ('雲林', 676873), ('高雄', 2765932), ('屏東', 812658)]

----------------------------------------------------

 list(zip(range(5), cities, population))

Out[6]: [(0, '台南', 1874917), (1, '雲林', 676873), (2, '高雄', 2765932), (3, '屏東', 812658)]

----------------------------------------------------

for ind, city, pop in zip(range(5), cities, population):
print(f"{ind}: {city}有{pop}人")

 

0: 台南有1874917人
1: 雲林有676873人
2: 高雄有2765932人
3: 屏東有812658人

----------------------------------------------------

cities = ["台南","雲林","高雄","屏東"]
new_cities=[]
for ind, city in zip(range(len(cities)),cities):
new_cities.append("第"+str(ind+1)+"個拜訪"+city)

print(f"{cities}")
print(f"{new_cities}")


['台南', '雲林', '高雄', '屏東']
['第1個拜訪台南', '第2個拜訪雲林', '第3個拜訪高雄', '第4個拜訪屏東']

----------------------------------------------------

cities = ["台南","雲林","高雄","屏東"]
print(cities)
# print(cities[0:len(cities)])
print(cities[::-1])

['屏東', '高雄', '雲林', '台南']

----------------------------------------------------

x=list(zip(range(10), cities))

[(0, '台南'), (1, '雲林'), (2, '高雄'), (3, '屏東')]


x[::-1]

[(3, '屏東'), (2, '高雄'), (1, '雲林'), (0, '台南')]

----------------------------------------------------

 

import pandas as pd

 

data = pd.read_csv("data/tw.csv")
coordinate = data.iloc[:,1:3].values
print(f"{coordinate}")

 

coordinate2 = coordinate[:,::-1]
print(f"{coordinate2}")

 

 

 ----------------------------------------------------

import numpy as np
x = [[1,2,3],[4,5,6],[7,8,9]]

 
y =np.mean(x, 0)
z =np.mean(x, 1)
m =np.mean(x)

  ----------------------------------------------------

import numpy as np
x=np.arange(4)
y=[np.arange(3),np.arange(3,6), np.arange(6,9)]

 

----------------------------------------------------

import numpy as np
import random
candidates = [i for i in range(0, 10)]
print('candidates:'+str(candidates))
i_selected = random.choice(candidates)
print('i_selected:'+str(i_selected))

candidates.remove(i_selected)
print('candidates:'+str(candidates))

i_selected = random.choice(candidates)
print('i_selected:'+str(i_selected))

candidates.remove(i_selected)
print('candidates:'+str(candidates))

 ----------------------------------------------------

import numpy as np
import random

candidates = np.arange(10, 20)
print(f"{candidates}")
ind_rand_sel = random.choice(candidates)

candidates = np.delete(candidates, candidates==ind_rand_sel)
print(f"After removing {ind_rand_sel}, {candidates}")

ind_rand_sel = random.choice(candidates)

candidates = np.delete(candidates, candidates==ind_rand_sel)
print(f"After removing {ind_rand_sel}, {candidates}")

----------------------------------------------------

import numpy as np

fitness = np.arange(1,11)
sum_fitness = fitness.sum()
fitness_norm = [fit/sum_fitness for fit in fitness]
print(f"fitness={fitness}")
print(f"sum_fitness={sum_fitness}")
print(f"fitness_norm={fitness_norm}")

 

----------------------------------------------------

import numpy as np
import random
fitness = np.arange(1,11)
sum_fitness = fitness.sum()
fitness_norm = [fit/sum_fitness for fit in fitness]
q = random.random()
print(f"q={q}")
sum_prob=0
for ind, prob in enumerate(fitness_norm):
sum_prob+=prob
if(sum_prob>q):
print(f"ind={ind},sum_prob={sum_prob}")
break
else:
print(f"ind={ind},sum_prob={sum_prob}")

 

 

----------------------------------------------------

import numpy as np

import random

rows = 3
cols = 4
x=np.ones([rows, cols])
print(f"{x}")
x=np.zeros([rows, cols])
print(f"{x}")
x=np.random.rand(rows, cols)
print(f"{x}")
x=np.random.randint(2, size=10)
print(f"{x}")
x=np.random.randint(1, 5, size=10)
print(f"{x}")

 

 

----------------------------------------------------

import numpy as np

import random
x = np.random.randint(1,11,size=2)
y = np.random.randint(1,11,size=2)
print(f"x={x}, y={y}")
z = np.sqrt(np.power(x-y,2).sum())
print(f"dist={z}")

---------------------------------------------------- 

 

 

 

 

 

 

 

----------------------------------------------------

 

 

 

 

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()