如何使用地板分割返回浮点值

时间:2019-03-20 16:40:13

标签: python-3.x floor-division

在Python 3中,我想返回整数值的单位位置,然后是十,然后是几百,依此类推。假设我有一个整数456,首先我想返回6,然后返回5,然后返回4。有什么办法吗?我尝试了楼层分割和for循环,但是没有用。

2 个答案:

答案 0 :(得分:0)

如果您查看文档中的基本运算符列表,例如here

val

有了这些知识,您可以按以下方式获得所需的内容:

    @Component
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    public class UserSyncListener implements InitializingBean, DisposableBean, ApplicationContextAware
    {
        private ApplicationContext applicationContext;
        private SubscriptionClient client;

        @Override
        public void afterPropertiesSet() throws ServiceBusException, InterruptedException
        {
            /* ... obtain connectionString and entityPath ... */
            client = new SubscriptionClient(
                new ConnectionStringBuilder(connectionString, 
                                            entityPath, 
                                            ReceiveMode.PEEKLOCK);
        }

        @Override
        public void destroy() throws Exception
        {
            if (client != null) {
                client.close();
            }
        }

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
        {
            this.applicationContext = applicationContext;
            if (applicationContext instanceof AbstractApplicationContext) {
                ((AbstractApplicationContext)applicationContext).registerShutdownHook();
            }
        }

答案 1 :(得分:0)

如果您要根据以下要求在代码的不同位置检索数字,请编写生成器

如果您对Python的生成器不太熟悉,请快速浏览https://www.programiz.com/python-programming/generator

  

»这里 get_digits()是生成器。

def get_digits(n):
    while str(n):
        yield n % 10

        n = n // 10
        if not n:
            break

digit = get_digits(1729)

print(next(digit)) # 9
print(next(digit)) # 2
print(next(digit)) # 7
print(next(digit)) # 1
  

»如果要遍历数字,也可以按照以下步骤进行。

for digit in get_digits(74831965):
    print(digit)

# 5
# 6
# 9
# 1
# 3
# 8
# 4
# 7
  

»有关其用法的快速概述(在Python3的Interactive终端上)。

>>> def letter(name):
...     for ch in name:
...         yield ch
... 
>>> 
>>> char = letter("RISHIKESH")
>>> 
>>> next(char)
'R'
>>> 
>>> "Second letter is my name is: " + next(char)
'Second letter is my name is: I'
>>> 
>>> "3rd one: " + next(char)
'3rd one: S'
>>> 
>>> next(char)
'H'
>>>