Append two arrays together into one? (Numpy/Python)

2024/7/5 10:54:02

I currently have an array of strings and I'm trying to join it together with another array of strings to form a complete word to do some web parsing. For example:`

Var1    [A   B    C, .... ....]
Var2    [1   2     3]

where A and B are at a variable length and I'm trying to join them together like:``

`
C  [A+1   A+2   A+3B+1    B+2   B+3
C+1     C+2    C+3

Here's what I've tried

for param in np.nditer(Var2):
List = np.append(np.core.defchararray.add(Var1, Var2))

So I'm trying to add them together and then create a list of lists, but this isn't working. any ideas how to do this?

Answer

Is this what you are trying to do:

In [199]: list1 = ['abc','foo','bar']
In [200]: list2 = list('1234')
In [201]: [[a+b for b in list2] for a in list1]
Out[201]: 
[['abc1', 'abc2', 'abc3', 'abc4'],['foo1', 'foo2', 'foo3', 'foo4'],['bar1', 'bar2', 'bar3', 'bar4']]

The equivalent using np.char.add and broadcasting:

In [210]: np.char.add(np.array(list1)[:,None], np.array(list2))
Out[210]: 
array([['abc1', 'abc2', 'abc3', 'abc4'],['foo1', 'foo2', 'foo3', 'foo4'],['bar1', 'bar2', 'bar3', 'bar4']], dtype='<U4')

For this small example the list comprehension version is faster.

https://en.xdnf.cn/q/120095.html

Related Q&A

Python 2.7 Isolate multiple JSON objects in a string

Ive to parse a text file that contains different kind of data. The most challenging is a line that contains three different JSON object (strings) and other data between them. Ive to divide the Json da…

pass different C functions with pointer arrays as the function argument to a class

I am trying to pass different functions which have pointers as arguments to a python function. One example of the input function as input parameter is the given normal function: Sample.pyxfrom cpython …

dynamic filter choice field in django

I am using django-filter to filter results on a page. I am able to use static choice filters like this:filters.pyFILTER_CHOICES = ( (, All States), (AL, Alabama), (AK, Alaska), (AZ, Arizona), #and so f…

How to print a table from a text file and fill empty spaces?

I have the following table in a txt file:|Client | Container weight | Country of Origin | Product code ||S4378 | 450 Ton | China | 457841 || | 350 Ton | Japan…

Open a file in python from 2 directory back

I want to read a file from 2 folders back.. with open(../../test.txt, r) as file:lines = file.readlines()file.close()I want to read from ../../ two folders back. but not work.. How i can do that ?

Do string representations of dictionaries have order in Python 3.4?

I know dictionaries themselves in Python do not have order. However, Im rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no mat…

BeautifulSoup Scraping Results not showing

I am playing around with BeautifulSoup to scrape data from websites. So I decided to scrape empireonlines website for 100 greatest movies of all time. Heres the link to the webpage: https://www.empireo…

How to verify username and password from CSV file in Python?

I am doing a Python project where I have to verify my username and password from a csv file where the first two rows and columns have the username and password as hi.Current Code: answer = input("…

adding validation to answer in quiz gives wrong answers

I am a complete novice with Python and working on a multiple choice quiz that reads questions from a file and keeps a score that then writes to a file. Everything was working perfectly until I added v…

Why do I get None as the output from a print statement? [duplicate]

This question already has answers here:Why is "None" printed after my functions output?(7 answers)Closed 2 years ago.def print_name(name):print(name)print(print_name(Annabel Lee))Why do I ge…