从列表中删除第一个字符

2024-05-01 21:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个程序,让用户输入他们想要的晚餐,然后输出一个购物清单。目前,用户可以输入他们想要的食物,它将打印出列表排序,从生产,肉,和其他。你知道吗

我希望程序输出的材料没有在前面的类别编号,并在每个条目后有一个换行符,但我有一些问题处理列表,而不是字符串。到目前为止,我已经尝试用正则表达式来代替数字,或者用空格来代替数字。你知道吗

加分,如果有人知道一种方法进入玉米片两次,打印出2xchicken而不是chicken两次。你知道吗

enter code here

strog = ["3 egg noddles", "3 beef broth", "2 steak"]
c_soup = ["2 bone in chicken", "1 carrots", "1 celery", "1 onion", "1     parsley"]
t_soup = ["3 tomato saucex2", "3 tomato paste", "1 celery"]
nachos = ["3 chips", "3 salsa", "3 black olives", "2 chicken", "3 cheese"]
grocery = []
done = []
food = ""
while food != done:
    food = input("Please enter what you would like to eat. Enter done when finished: ")
    grocery += (food)
grocery.sort()
print(grocery)

Tags: 用户程序列表food数字购物celery食物
2条回答

似乎您只需要学习普通的python string manipulation和list<;->;字符串转换(splitjoin)。他说

试着这样做:

strog = ["3 egg noddles", "3 beef broth", "2 steak"]

for ingredient in strog:
    print(" ".join(ingredient.split()[1:]))

或不丑:

strog = ["3 egg noddles", "3 beef broth", "2 steak"]

for ingredient in strog:
    pieces_list = ingredient.split()
    food_list = pieces_list[1:]
    ingredient_without_number = " ".join(food_list)
    print(ingredient_without_number)

您的代码还有一些其他问题,但是您可以使用(例如)strog[0][2:]在strog中获取一个项目,或者通过执行new_strog = [x[2:] for x in strog]解析出类别号和空格来获取整个列表。他说

相关问题 更多 >