如何从长十进制值列表中选择最后 3 位数字
how to pick last 3 digits from the list of long decimal values
下面是十进制数列表;
000000 = 0.0218354767535
000001 = 0.0218265654136
000002 = 0.0218184623573
000003 = 0.021811165579
000004 = 0.0218046731276
000005 = 0.0217989831063
000006 = 0.0217940936718
000007 = 0.0217900030345
000008 = 0.0217867094577
000009 = 0.0217842112574
000010 = 0.021782506802
我想获取给定列表中每个十进制数的最后三位数字。以下是我在这个例子中的需要。
535
136
573
579
276
063
718
345
577
574
802
对于可能的解决方案有什么建议吗?
对于每一行,您只能像这样索引最后三个元素:
with open('filename') as infile:
for line in infile:
print line.rstrip()[-3:]
假设给定的列表是一个字符串列表。在这种情况下,请使用此代码。
l1 = ["000000 = 0.0218354767535",
"000001 = 0.0218265654136",
"000002 = 0.0218184623573",
"000003 = 0.021811165579",
"000004 = 0.0218046731276",
"000005 = 0.0217989831063",
"000006 = 0.0217940936718",
"000007 = 0.0217900030345",
"000008 = 0.0217867094577",
"000009 = 0.0217842112574",
"000010 = 0.021782506802"]
for each_item in l1:
print each_item[-3:]
收到澄清后修改答案
result_list = [] # This would store the end result with int
# The below list is the input list with floats
l1 = [0.0218354767535,
0.0218265654136,
0.0218184623573,
0.021811165579,
0.0218046731276,
0.0217989831063,
0.0217940936718,
0.0217900030345,
0.0217867094577,
0.0217842112574,
0.021782506802]
#For each float in the list, find the decimal part, convert to string, extract last 3, convert to int and append to result list
for each_item in l1:
result_list.append(int(str(each_item-int(each_item))[-3:]))
print result_list
输出
[535, 136, 573, 579, 276, 63, 718, 345, 577, 574, 802]
下面是十进制数列表;
000000 = 0.0218354767535
000001 = 0.0218265654136
000002 = 0.0218184623573
000003 = 0.021811165579
000004 = 0.0218046731276
000005 = 0.0217989831063
000006 = 0.0217940936718
000007 = 0.0217900030345
000008 = 0.0217867094577
000009 = 0.0217842112574
000010 = 0.021782506802
我想获取给定列表中每个十进制数的最后三位数字。以下是我在这个例子中的需要。
535
136
573
579
276
063
718
345
577
574
802
对于可能的解决方案有什么建议吗?
对于每一行,您只能像这样索引最后三个元素:
with open('filename') as infile:
for line in infile:
print line.rstrip()[-3:]
假设给定的列表是一个字符串列表。在这种情况下,请使用此代码。
l1 = ["000000 = 0.0218354767535",
"000001 = 0.0218265654136",
"000002 = 0.0218184623573",
"000003 = 0.021811165579",
"000004 = 0.0218046731276",
"000005 = 0.0217989831063",
"000006 = 0.0217940936718",
"000007 = 0.0217900030345",
"000008 = 0.0217867094577",
"000009 = 0.0217842112574",
"000010 = 0.021782506802"]
for each_item in l1:
print each_item[-3:]
收到澄清后修改答案
result_list = [] # This would store the end result with int
# The below list is the input list with floats
l1 = [0.0218354767535,
0.0218265654136,
0.0218184623573,
0.021811165579,
0.0218046731276,
0.0217989831063,
0.0217940936718,
0.0217900030345,
0.0217867094577,
0.0217842112574,
0.021782506802]
#For each float in the list, find the decimal part, convert to string, extract last 3, convert to int and append to result list
for each_item in l1:
result_list.append(int(str(each_item-int(each_item))[-3:]))
print result_list
输出
[535, 136, 573, 579, 276, 63, 718, 345, 577, 574, 802]