社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Ishan Shishodiya

Ishan Shishodiya 最近创建的主题
Ishan Shishodiya 最近回复了
3 年前
回复了 Ishan Shishodiya 创建的主题 » 除了使用Python的集合之外,还使用If-Else语句

你可以使用这个代码,

fortune_num = list(range(1,10))
user_num = input(f'Pick a number and find your fortune!\nChoose a number from 1 to 9 and hit enter: ')

if int(user_num) in fortune_num:
  print(user_num)
else:
  raise ValueError("You entered a wrong input!")

如果输入错误,这将引发实际的Python错误。

如果你想让用户有机会输入正确的输入,请使用此选项,

fortune_num = list(range(1,10))

while True:
  try:
    user_num = int(input(f'Pick a number and find your fortune!\nChoose a number from 1 to 9 and hit enter: '))
    if user_num in fortune_num:
      print(user_num)
      break
    else:
      raise ValueError

  except:
    print("\nYou entered a wrong input. Please enter a valid number!")

根据你的需要修改代码,但这是一个完美的基础。

3 年前
回复了 Ishan Shishodiya 创建的主题 » 如何用python绘制条形图?

而不是使用 matplotlib 的条形图要绘制每个类的计数,您可以 seaborn 是的 countplot .

plt.figure(figsize = (7,5))
sns.countplot(x = "fake", data = df, palette=["blue","red"])
plt.show() 

输出-

enter image description here

3 年前
回复了 Ishan Shishodiya 创建的主题 » 替换python中在我的代码中不起作用的函数

这会管用的,

def rmv_spc(lst,a):  
  for i in range(len(lst)):
    lst[i] = str(lst[i]).replace(str(a),"")

  return lst 

print(rmv_spc([343, 893, 1948, 3433333, 2346],3))

输出-

['4', '89', '1948', '4', '246']

您的代码成功地替换了列表中每个项目的字符,但没有将旧项目替换为新项目。

3 年前
回复了 Ishan Shishodiya 创建的主题 » 用Python填写大小不均的列表

你得到的价值是零 newlist 因为 val.insert(1,0) 返回 None 作为输出。

试试这个,

lst = [[1,2,3],[-1,2,4],[0,2],[2,-3,6]]
new_list = [i + [0] if len(i) < 3 else i for i in lst]
print(new_list)

输出-

[[1, 2, 3], [-1, 2, 4], [0, 2, 0], [2, -3, 6]]