简体   繁体   English

python中的命令行参数

[英]Command line arguments in python

This is my problem: Through command line arguments three strings separated by space are given to you.Each string is a series of numbers separated by a hyphen(-).这是我的问题:通过命令行参数为您提供了三个由空格分隔的字符串。每个字符串是一系列由连字符 (-) 分隔的数字。 You like all the numbers in string 1 and dislike all the numbers in string 2.Third string contains the numbers given to you.your initial happiness is 0. When you encounter a number present in string 1,add 1 to your happiness,if it is present in string 2,add -1 to your happiness.Otherwise your happiness does not change.Output your final happiness at the end.你喜欢字符串 1 中的所有数字,不喜欢字符串 2 中的所有数字。第三个字符串包含给你的数字。你的初始快乐是 0。当你遇到字符串 1 中的数字时,给你的快乐加 1,如果它出现在字符串2中,给你的幸福加-1。否则你的幸福不会改变。最后输出你最终的幸福。

Sample input: 3-1 5-7 1-5-3-8
Sample output: 1

Explanation: Numbers in string 1: 3,1.说明:字符串 1 中的数字:3,1。 Numbers in string 2: 5,7.字符串 2 中的数字:5,7。 Numbers given to you: 1,5,3,8.给你的数字:1、5、3、8。 1 unit of happiness is gained for numbers 1 and 3 which are in string 1.Your total happiness is 2 now.字符串 1 中的数字 1 和 3 获得 1 个单位的幸福感。您现在的总幸福感是 2。 You lose 1 unit of happiness for number 5 which is in string 2. Your total happiness is 1 now.8 is not present in either of strings, so happiness does not change.Final happiness is 1.对于字符串 2 中的数字 5,您失去了 1 个单位的幸福感。现在您的总幸福感是 1。8 不在任何一个字符串中,因此幸福感不会改变。最终的幸福感是 1。

This is what I understood.这是我的理解。 My code for this:我的代码:

from sys import argv
print("Number in string1:",argv[1])
print("Number in string2:",argv[2])
print("Number in string3:",argv[3])
if i in argv[3]== i in argv[1]:
    count=count+1
if i in argv[3]==i in argv[2]:
    count=count-1
print("Happiness=",count)

But this doesn't work.Please do help me out.I am beginner and command line arguments are really confusing.Thanks in advance.但这不起作用。请帮帮我。我是初学者,命令行参数真的很混乱。提前致谢。

Frist of all, you will want the take the strings given in the command line arguments and turn them into lists:首先,您需要获取命令行参数中给出的字符串并将它们转换为列表:

liked_numbers = argv[1].split("-")
disliked_numbers = argv[2].split("-")
given_numbers = argv[3].split("-")

Using your sample input this will give you lists in the following format:使用您的示例输入,这将为您提供以下格式的列表:

liked_numbers = ['3', '1'] like_numbers = ['3', '1']

disliked_numbers = ['5', '7'] disliked_numbers = ['5', '7']

given_numbers = ['1', '5', '3', '8'] given_numbers = ['1', '5', '3', '8']

Then, you should compare each of your given numbers to the other lists.然后,您应该将每个给定的数字与其他列表进行比较。 The following code will first take a number from given_numbers and search for it in liked_numbers, then it will search for it in disliked_numbers, it will do this for every number in given_numbers:以下代码将首先从 given_numbers 中获取一个数字并在 like_numbers 中搜索它,然后它将在 disliked_numbers 中搜索它,它将对 given_numbers 中的每个数字执行此操作:

happiness = 0
for num in given_numbers:
    if num in liked_numbers:
        happiness += 1
    if num in disliked_numbers:
        happiness -= 1
print(happiness)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM