import numpy as np
names
names = np.array(['Apple Inc', 'Coca-Cola', 'Walmart'])
negative_index = np.array([-1,-2])
Use slicing on list names
names_subset = names[negative_index]
print(names_subset)
注意哪里中括號(hào)哪里小括號(hào)右钾,哪里逗號(hào)哪里冒號(hào)
這個(gè)做法 可以先顯示最后一個(gè) 再顯示倒數(shù)第二個(gè)
結(jié)果: ['Walmart' 'Coca-Cola']
For lists, useful functions include max() and min(), which identify the maximum or minimum value in a list. A useful list method is .sort() which sorts the elements in a list.
prices = [159.54, 37.13, 71.17]
price_max = max(prices) max是一個(gè)函數(shù)
但是method功能的話 是prices_min = prices.sort() 放在點(diǎn)變量的后面
names = ['Apple Inc', 'Coca-Cola', 'Walmart']
names.append('Amazon.com')
print(names)
['Apple Inc', 'Coca-Cola', 'Walmart', 'Amazon.com'] 是加一個(gè)元素瑞佩,元素的總數(shù)量增加了
.extend() 可以在后面添加多個(gè)元素
.index 返還的是 index數(shù)字
np.pmt() 企業(yè)年金
rf = pd.DataFrame.from_dict(tt, orient="index");
rf.to_csv("LocalDrive\\Sales.csv")
計(jì)算加權(quán)平均數(shù)~~
# Create the combined list for sales and probability
sales_probability = ['0|0.05', '200|0.1', '300|0.4', '500|0.2', '800|0.25']
weighted_probability = 0
# Create a for loop to calculate the weighted probability
for pair in sales_probability:
parts = pair.split('|')
weighted_probability += float(parts[0]) * float(parts[1])
# Print the weighted probability result
print("The weighted probability is {}.".format(weighted_probability))