Adding numbers to a string sequentially

I am a new programmer learning python and I am having problems with how to approach this task:

So essentially I have a string of numbers to read imported from the file, and you need to add the sum of the first number to the second and convert it to the correct ascii character. So, for example, if I read the line:

'36 30 25 40 80 4' 

I would like to add from 36 to 30, then from 25 to 40, then from 80 to 4, etc., before converting them to the corresponding ASCII character and printing (so in this case "CAT").

So far, I understand that I need to use the split command to split the string, and then the for loop to receive data for consecutive numbers. I can get the correct value for the first letter, but I had problems with the "message" to the program so that it automatically adds [2] to [3] after adding [1] to [0]. I also need to do this for multiple lines.

Again, I am a new coder, so any help is greatly appreciated. Thank!

+4
source share
11 answers

Another method that is less opaque (albeit more verbose) than Patrick's suggestion would be:

>>> x = '36 30 25 40 80 4'
>>> nums = [int(n) for n in x.split()]
>>> ''.join(chr(sum(nums[i:i+2])) for i in range(0, len(nums), 2))
'BAT'
+2
source

You can use special use zipto combine elements:

s = '36 30 25 40 80 4'
ints = map(int, s.split()) #make into list of ints
sums = map(sum, zip(*[iter(ints)]*2))
print(''.join([chr(i) for i in sums]))

: iter , , , .

2: jsbuenos :

map (, ) . , list(map(int, ['1', '2'])) [int('1'), int('2')]. list , map , , .

zip , . . , iter, ints map , , , . ,

sums = map(sum, zip(*[ints]*2))

, map, , .

zip(*[ints]*2)

, .

zip(*[ints, ints])

* . , ,

zip(ints, ints)

, zip? zip , , .

list(zip([1,2,3], [4,5,6])) #put the results from zip into a list
[(1, 4), (2, 5), (3, 6)]

, , .

, zip(ints, ints) ints . , zip , , ints. 36. , ints. , 30. .

: " zip(*[iter(ints)]*2), zip(ints, ints) ?" , , . zip(iter(ints),iter(ints)) , iter , , . , iter, , , .

+3

ascii, , "BAT" "CAT".

>>> s = '36 30 25 40 80 4'
>>> ints = [int(n) for n in s.split()]             # convert strings to integers
>>> summed = [x+y for x,y in zip(ints[0::2], ints[1::2])]        # summing pairs
>>> bytearray(summed).decode('ascii')                   # decoding to ascii text
'BAT'
+2

:

In [18]: str='36 30 25 40 80 4'

In [19]: nums=str.split()

In [20]: for i in range(0, len(nums), 2):
    print(int(nums[i]) + int(nums[i+1]))
   ....:     
66
65
84

, , len (nums) . IndexError.

+1

, comprehesnion :

>>> my_string = '36 30 25 40 80 4'
>>> num_list = [int(num) for num in my_string.split()] # list of numbers
>>> ''.join(chr(sum(c_num)) for c_num in zip(num_list[:-1:2], num_list[1::2]))
'BAT'     #                                    ^ iterate simultaneous value with step 2
0

2

    for i in range(0, 6, 2):
        do something to el[i] and el[i+1]

, . : [0, 2, 4]

0

:

string = '36 30 25 40 80 4'
numbers = [int(e) for e in string.split()]
res = ''.join([chr(numbers[i]+numbers[i+1]) for i in range(0,len(numbers),2)])
print(res)

: BAT

0

.

>>> t = "36 30 25 40 80 4"
>>> t_s = map(int,t.split())
>>> t_s
[36, 30, 25, 40, 80, 4]
>>> "".join([chr(sum(c)) for c in zip(t_s[1::2],t_s[::2])])
'BAT'
0

:

s = '36 30 25 40 80 4'
a = [int(x) for x in s.split()] #convert to integers
firsthalf = a[::2] #every other starting from 0
secondhalf = a[1::2] #every other starting from 1

zip , :

pairs = zip(firsthalf, secondhalf)

sum paris:

ordinals = [sum(x) for x in pairs]

, :

print(''.join([chr(x) for x in ordinals])) #prints: 'BAT'
0

, , :

>>> s = '36 30 25 40 80 4'
>>> i = iter(map(int, s.split()))
>>> ''.join(chr(sum(x)) for x in zip(i, i))
'BAT'
0

You can convert the string to an iterator that returns ints and use the function nextinside the list comprehension:

s = '36 30 25 40 80 4' 

# iter is not necessary in python3
itr = iter(map(int, s.split()))
word = ''.join(chr(a+next(itr)) for a in itr)

print(word)
# BAT

IMO is a little cleaner because there is no explicit indexing.

0
source

Source: https://habr.com/ru/post/1660222/


All Articles