鱼C论坛

 找回密码
 立即注册
查看: 1554|回复: 5

[已解决]将多位整数转成列表

[复制链接]
发表于 2020-10-13 15:00:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

# 第一个问题
# nums = [2,5,3,8]

# 输出为:[5,5,8,8,8]  2个5,3个8


# # 第二个问题
# nums = [42,39]        
# 输出为:[39,39,39,39,39,39,39,39,39,39,39,39,39......]   一共42个39


第一个基本会做了,但用第一个的代码运行第二个会先显示
['3', '9', '3', '9', '3', '9', '3', '9', '3', '9', '3', '9', '3', '9']


有没有大佬会这个的
最佳答案
2020-10-13 15:15:25
jtxs0000 发表于 2020-10-13 15:07
好吧,应该是我没有描述清楚,我是想要写一组代码,能把第一个问题和第二个问题都能运行起来,甚至说,如 ...

这样?
nums = [42,39,12,5,100,23]
new=[nums[i+1] for i in range(0,len(nums),2) for a in range(1,nums[i]+1)]
print(new)

[39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-13 15:03:38 | 显示全部楼层
>>> nums = [42,39]
>>> print([nums[1]] * nums[0])
[39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39]

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
jtxs0000 + 1 + 1 谢谢大佬给我思路

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-10-13 15:07:56 | 显示全部楼层

好吧,应该是我没有描述清楚,我是想要写一组代码,能把第一个问题和第二个问题都能运行起来,甚至说,如果出现其他类似的也可以运行,比如
nums = [42,39,12,5,100,23]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-13 15:15:25 | 显示全部楼层    本楼为最佳答案   
jtxs0000 发表于 2020-10-13 15:07
好吧,应该是我没有描述清楚,我是想要写一组代码,能把第一个问题和第二个问题都能运行起来,甚至说,如 ...

这样?
nums = [42,39,12,5,100,23]
new=[nums[i+1] for i in range(0,len(nums),2) for a in range(1,nums[i]+1)]
print(new)

[39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-13 15:19:22 | 显示全部楼层
jtxs0000 发表于 2020-10-13 15:07
好吧,应该是我没有描述清楚,我是想要写一组代码,能把第一个问题和第二个问题都能运行起来,甚至说,如 ...
nums = [42, 39, 12, 5, 100, 23]
output = []
for each in range(0, len(nums), 2):
    output += nums[each] * [nums[each + 1]]
print(output)

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
jtxs0000 + 1 + 1 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-13 15:29:33 | 显示全部楼层
num = [5,3,2,4,2,8]
l = len(num)
nums = []
if l%2==0:
    #偶数输出列表
    for i in range(0,l):
        if i%2!=0:
            nums.extend([num[i]] * num[i-1])
else:
    #奇数报错
    print("少一个数字")
print(nums)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-18 09:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表