prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
rev = list(zip(prices.values(), prices.keys()))
print(rev)
min_price = min(rev)
print(min_price)
print(max(prices.values()))
a = [1, 5, 6, 7, 2, 1, 5, 6, 9]
a_set = set(a)
a = list(a_set)
print(a)
#########################################
my_slice = slice(5)
my_slice2 = slice(2, 10, 2)
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(arr[my_slice]) # [0, 1, 2, 3, 4]
print(arr[my_slice2]) # [2, 4, 6, 8]
print(my_slice.start, my_slice.stop, my_slice.step)
print(my_slice2.start, my_slice2.stop, my_slice2.step)
###### 0123456789012345678901234567890123456789012345678901234567890'
record = '....................100 .......513.25 ..........'
# cost = int(record[20:23]) * float(record[31:37])
SHARES = slice(20, 23)
PRICE = slice(31, 37)
cost = int(record[SHARES]) * float(record[PRICE])
print(cost)
#########################################
# ###################################### 查找出现最多的单词
words = ['look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not',
'around', 'the', 'around', 'eyes', "do", 'my', 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes',
"you", 'under'
]
a_set = set(words)
a_list = list(a_set)
count = []
for i in a_list:
count.append(words.count(i))
aa_list = list(zip(a_list, count))
print(max(aa_list, key=lambda x: x[1]))
#############################################
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
a_list = [x for x in mylist if x > 0] # 只保留大于 0 的数
b_list = [x for x in mylist if x % 2] # 只保留奇数
print(a_list, b_list)
######################################
values = ['1', '2', '-3', '-', '2.6', '4', 'N/A', '5']
def is_int(x):
try:
int(x)
return True
except Exception:
return False
result = list(filter(is_int, values)) # ['1', '2', '-3', '4', '5']
print(result)
##################################
##################################
mylist = [1, 4, -5, 10, -7, 2, 3, -1, 0]
import math
sq = [format(math.sqrt(x), '0.3f') for x in mylist if x >= 0]
print(sq)
##################################
##################################
addresses = [
'5412 N CLARK',
'5148 N CLARK',
'5800 E 58TH',
'2122 N CLARK',
'5645 N RAVENSWOOD',
'1060 W ADDISON',
'4801 N BROADWAY',
'1039 W GRANVILLE',
]
counts = [0, 3, 10, 4, 1, 7, 6, 1]
counter = 0
for i in counts:
if i > 5: # 删选出 counts 大于5的地址
print(addresses[counter], i)
counter += 1
##################################
a, b = 4.2, 2.1
print(a + b) # 6.300000000000001
from decimal import Decimal
a1, b1 = Decimal('4.2'), Decimal('2.1')
print(a1 + b1) # 6.3
a, b = 4.2, 2.1
a, b = str(a), str(b)
a = a.split('.')
b = b.split('.')
for i in (a, b):
print(i)
print(a, b)
########################################################
#########################################################
from fractions import Fraction
a, b = Fraction(1, 3), Fraction(2, 5) # 分数 1/3 2/5
print(a + b) # 11/15
print(a - b) # -1/15
c = a * b # 2/15
print(c, c.numerator, c.denominator) # numerator 分子,denominator 分母
########################################################
####################################### 字典翻转,价格如有相同的,后面的会覆盖前面的
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75,
}
new_ = {v:k for k,v in prices.items()} # {45.23: 'ACME', 612.78: 'AAPL', 205.55: 'IBM', 37.2: 'HPQ', 10.75: 'FB'}
print(max(new_))
print(min(new_))
#########################################
Some codes
最新推荐文章于 2024-10-15 19:28:18 发布