[Python] A program that compares each element of list one by one and wins or loses. zip ()

[Python] A program that compares each element of list one by one and wins or loses. zip ()

When two arrays are given, the first element of each is compared, the winner is given 1 point, and the score of each is output.

Alice vs Bob. One point will be given to the winner. 0 points for a draw.

▼sample input

17 28 30
99 16 8

▼sampel output

2 1

▼my answer

python



def compareTriplets(a, b):
#Set variables for counting the number of wins and losses. Initial value 0
    alice=0
    bob=0

#Compare the elements of a given array one by one
    for pair in zip(a,b):
        if pair[0]>pair[1]:
            alice+=1
        elif pair[0]<pair[1]:
            bob+=1

#Returns a return value
    return (alice,bob)

#input processing
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    a = list(map(int, input().rstrip().split()))

    b = list(map(int, input().rstrip().split()))

    result = compareTriplets(a, b)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()

Extract elements from multiple arrays one by one

Use zip (array 1, array 2) The type is zip. The first element of array 1 and the first element of array 2 are set in a nested structure, and a new element is used as the first element.

python


a=[1,2,3]
b=[4,5,6]
print(list(zip(a, b)))

#output
[(1, 4), (2, 5), (3, 6)]

Combination of zip and for statement

python


a=[1,2,3]
b=[4,5,6]

for pair in zip(a,b):
    print(pair)

#output
(1, 4)
(2, 5)
(3, 6)

rstrip() Remove trailing whitespace

a="   a bcd efg     "
a.rstrip()

#output
'   a bcd efg'

Remove whitespace at the beginning and end of sentences

.strip () Remove leading and trailing spaces .lstrip () Remove leading whitespace .rstrip () Remove trailing whitespace

python


letter="   a bcd efg     "

s = letter.strip()
l = letter.lstrip()
r = letter.rstrip()

print(s)
print(l)
print(r)

#output
a bcd efg ← Up to here
a bcd efg ← Up to here
a bcd efg ← Up to here

Delete the specified characters at the beginning and end of the sentence

.strip ("character string ") Start and end .lstrip ("character string ") beginning .rstrip ("character string ") end

python


letter="a bad eaa"

s = letter.strip("a")
l = letter.lstrip("a")
r = letter.rstrip("a")

print(s)
print(l)
print(r)

#output
 bad e
 bad eaa
a bad e

Method output definition

①return If the call has fptr = open (os.environ ['OUTPUT_PATH'],'w'). The method is assigned to a variable. result = compareTriplets (a, b)

②print If you are executing a method. If the final output is compareTriplets (a, b), print () is used in the method.

Recommended Posts

[Python] A program that compares each element of list one by one and wins or loses. zip ()
[Python] A program that compares the positions of kangaroos.
[Python] A program that rotates the contents of the list to the left
Connect a lot of Python or and and
[Python] A program that calculates the number of updates of the highest and lowest records
A program that sends a fixed amount of mail at a specified time by Python
[Python] A program that counts the number of valleys
Group by consecutive elements of a list in Python
A library that monitors the life and death of other machines by pinging from Python
[Python] A program to find the number of apples and oranges that can be harvested
[Python] How to make a list of character strings character by character
Make a table of multiplication of each element in a spreadsheet (Python)
[Python] A program that creates a two-dimensional array by combining integers
A Python script that compares the contents of two directories
python note: map -do the same for each element of the list
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
This and that of python properties
[Python] return A [or / and] B
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that finds a pair that can be divided by a specified value
[Python] A program that calculates the number of socks to be paired
A python program that resizes a video and turns it into an image
How to format a list of dictionaries (or instances) well in Python
[Python] A program that creates stairs with #
Display a list of alphabets in Python 3
OR the List in Python (zip function)
[python] Get a list of instance variables
A program that plays rock-paper-scissors using Python
[Python] Get a list of folders only
[Python] A program that rounds the score
A story made by a person who has no knowledge of Python or Json
[Python] Create a list of date and time (datetime type) for a certain period
Get a list of articles posted by users with Python 3 Qiita API v2
[python] How to sort by the Nth Mth element of a multidimensional array
Publishing and using a program that automatically collects facial images of specified people
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] A program that finds the minimum and maximum values without using methods
Get the last element of the array by splitting the string in Python and PHP