小编rgg*_*god的帖子

可以在python中将列表转换为队列吗?

如何将列表转换为队列?这样就可以进行入队或出队等操作.我想使用列表删除最顶层的值,我相信它可以使用队列完成.

python

15
推荐指数
4
解决办法
2万
查看次数

使用python查找整数中数字总和的递归函数

def sumdigits(number):
  if number==0:
    return 0
  if number!=0:
    return (number%10) + (number//10)
Run Code Online (Sandbox Code Playgroud)

这是我的功能。然而,它只给出 2 位数字的正确总和。我怎样才能得到任何数字的总和。我的函数也会算作递归吗

def main():
    number=int(input("Enter a number :"))
    print(sumdigits(number))
main()
Run Code Online (Sandbox Code Playgroud)

python recursion function

4
推荐指数
2
解决办法
3万
查看次数

除了在python中,如何使用try打开文件?

我有一个txt文件,我想在我的程序中使用,名为animallog1.txt。我如何打开它,以便它可以使用 try 和 except 在我的程序中使用。我知道如何使用 open 函数使用文件,但不使用 try except。有人能告诉我它是怎么做的吗

python

3
推荐指数
2
解决办法
1万
查看次数

如何从列表中的字符串末尾删除'\n'?

我这里有2个字符串:

line= ['ABDFDSFGSGA', '32\n']
line= ['NBMVA\n']
Run Code Online (Sandbox Code Playgroud)

如何\n从这些字符串的末尾删除.我已经尝试了rstrip(),strip()但我仍然无法\n从字符串中删除.我可以帮忙删除它吗?

python

2
推荐指数
3
解决办法
2万
查看次数

使用行星数据创建字典

我有太多关于太阳系中行星的细节.我本来应该用Python制作一本字典.对于每个行星,我有它的半径,与太阳的距离,卫星的数量,是否存在大气层,卫星的名称,是否是气体行星等等.

例如,以下是Mercury的数据:

Mercury
    Radius - 2,439.7 km
    Distance from the sun - 58 million km
    Moons - none
    Atmosphere? True
    Gas planet? False
Run Code Online (Sandbox Code Playgroud)

我如何使用所有这些数据来创建字典?

到目前为止,我有:

radius = {} #radius of planets
radius['Mercury'] = 2439.7
radius['Venus'] = 6051.8
radius['Earth'] = 6371.0
radius['Mars'] = 3,396.2
radius['Jupiter'] = 69,911
radius['Saturn'] = 60,268
radius['Uranus'] = 25,559
radius['Neptune'] = 24,764

distance = {} # distance from sun
distance['Mercury'] = 58000000
distance['Venus'] = 108000000
Run Code Online (Sandbox Code Playgroud)

我打算继续这个,为我拥有的所有数据创建一个字典,这样我就可以为每种不同类型的数据设置不同的部分.

但是,如果这是正确的方法,我不会这样做.有人能告诉我,我是否朝着正确的方向前进?如果没有,我该如何解决?

python dictionary

0
推荐指数
1
解决办法
250
查看次数

如何修复python代码中的错误?

def main():
    fname = input("Enter name of file: ")
    with open(fname) as inf:
        animalnames, dates, locations = zip(*[line.strip().split(':') for line in inf])

    d = {}
    for animalname, loc in zip(animalname, locations):
        d.setdefault(animalname, []).append(loc)  

    for k, v in d.items():
       print(k, end='\t')
       print(v.count('loc1'), end='\t')
       print(v.count('loc2'))        

main()
Run Code Online (Sandbox Code Playgroud)

我有一个txt文件名animallog1.txt,其中包含以下内容

a01:01-24-2011:s1 
a03:01-24-2011:s2 
a02:01-24-2011:s2 
a03:02-02-2011:s2 
a03:03-02-2011:s1 
a02:04-19-2011:s2 
a01:05-14-2011:s2 
a02:06-11-2011:s2 
a03:07-12-2011:s1 
a01:08-19-2011:s1 
a03:09-19-2011:s1 
a03:10-19-2011:s2 
a03:11-19-2011:s1 
a03:12-19-2011:s2  
Run Code Online (Sandbox Code Playgroud)

我想使用上面的数据,其格式为animaname:data:location来打印下表:

Number of times each animal visited each station : 
Animal name Station 1 Station 2 
a01         2         1 
a02         0 …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
80
查看次数

从python中的列表中删除空格和\n?

list1= ['34 5\n', '67 37\n', '40 33\n', '99 100\n', '55 22'] 
Run Code Online (Sandbox Code Playgroud)

以上是我所拥有的清单,我该如何做到

['34','5','67','37','40','33','99','100','55','22']
Run Code Online (Sandbox Code Playgroud)

我想删除空格和'\n'.rstrip,strip,replace已经尝试但没有工作过

list1.rstrip('\n')
list1.strip('\n')
list1.remove('\n')
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
71
查看次数

标签 统计

python ×7

dictionary ×1

function ×1

recursion ×1