J
J
Joe Weiser2017-04-22 00:40:14
Python
Joe Weiser, 2017-04-22 00:40:14

How to multiply text values ​​of columns?

Hello!
In general, the task is as follows:
There are two text files, one of which contains a list of names, and the other - a list of patronymics. It is necessary to create all possible combinations of first name + patronymic.
Please tell me the solution.
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Yarkov, 2017-04-22
@Joe_Weiser

names.txt

Иван
Степан

Иванович
Степанович

#!/usr/bin/env python
# -*- coding: utf-8 -*-

names_file = 'names.txt'
father_names_file = 'father_names.txt'
combinations_file = 'combinations.txt'

def main():
    with open(combinations_file, 'w') as combinations:
        with open(names_file, 'r') as names, open(father_names_file, 'r') as father_names:
            names_lines = names.readlines()
            father_names_lines = father_names.readlines()
            for name in names_lines:
                for father_name in father_names_lines:
                    line = "%s %s" % (name.replace("\n", ""),
                                      father_name.replace("\n", ""))
                    combinations.write("%s\n" % line)

if __name__ == '__main__':
    main()

Иван Иванович
Иван Степанович
Степан Иванович
Степан Степанович

O
Oleg, 2017-04-22
@politon

Option on js https://jsfiddle.net/Politonius/uh2fLao0/
get data in your format (with commas or just a space) and process as you like

I
Islam Ibakaev, 2017-04-22
@devellopah

maybe so

const combine = (names, lastNames) =>
  names.map(name => 
    lastNames.map(lastName => name + ' ' + lastName)
  );


combine(['John', 'Kenny', 'Mark'], ['Jackson', 'Duglas', 'Kirk']);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question