64

I'm relatively new to Python and it's libraries and I was wondering how I might create a string array with a preset size. It's easy in java but I was wondering how I might do this in python.

So far all I can think of is

strs = ['']*size

And some how when I try to call string methods on it, the debugger gives me an error X operation does not exist in object tuple.

And if it was in java this is what I would want to do.

String[] ar = new String[size];
Arrays.fill(ar,"");

Please help.

Error code

    strs[sum-1] = strs[sum-1].strip('\(\)')
AttributeError: 'tuple' object has no attribute 'strip'

Question: How might I do what I can normally do in Java in Python while still keeping the code clean.

17
  • 1
    Why are you trying to create an array of empty strings? Python usually has better ways to deal with string arrays. Commented Jun 16, 2011 at 18:43
  • 8
    For your basic issue, read this: dirtsimple.org/2004/12/python-is-not-java.html Commented Jun 16, 2011 at 18:46
  • 2
    Python is not slower at running things. You are just not good at programming it. Commented Jun 16, 2011 at 18:53
  • 2
    Exactly. Bad java habits are bad Commented Jun 16, 2011 at 19:01
  • 6
    Accidentally right. Running similar code in Python has a much higher overhead (regardless of implementation, only the exact difference changes), but that's not because some code you barely got running on Python doesn't run as fast as a decent Java program. Show your code and I'll propably find five things one can optimize about it while actually making it more idiomatic and readable. And to cite an old metaphor: would you rather spend five hours getting a program in a static language to work and watch it complete in a second or have a coffee while the 10 minutes script takes its 10 seconds? ;) Commented Jun 16, 2011 at 19:05

11 Answers 11

99

In python, you wouldn't normally do what you are trying to do. But, the below code will do it:

strs = ["" for x in range(size)]
Sign up to request clarification or add additional context in comments.

2 Comments

There won't be any tuple generated by this. Show more of course code.
This does the trick but rapidly gets ugly for multidimensional arrays!
31

In Python, the tendency is usually that one would use a non-fixed size list (that is to say items can be appended/removed to it dynamically). If you followed this, there would be no need to allocate a fixed-size collection ahead of time and fill it in with empty values. Rather, as you get or create strings, you simply add them to the list. When it comes time to remove values, you simply remove the appropriate value from the string. I would imagine you can probably use this technique for this. For example (in Python 2.x syntax):

>>> temp_list = []
>>> print temp_list
[]
>>> 
>>> temp_list.append("one")
>>> temp_list.append("two")
>>> print temp_list
['one', 'two']
>>> 
>>> temp_list.append("three")
>>> print temp_list
['one', 'two', 'three']
>>> 

Of course, some situations might call for something more specific. In your case, a good idea may be to use a deque. Check out the post here: Python, forcing a list to a fixed size. With this, you can create a deque which has a fixed size. If a new value is appended to the end, the first element (head of the deque) is removed and the new item is appended onto the deque. This may work for what you need, but I don't believe this is considered the "norm" for Python.

Comments

9

The simple answer is, "You don't." At the point where you need something to be of fixed length, you're either stuck on old habits or writing for a very specific problem with its own unique set of constraints.

3 Comments

True as advice to a beginner. There are a few cases where it's a valid coice to generate a list of N items and manipulate those items based on indiced without worrying to add items on a by-need basis first. But since half of these manipulate the items, OP's example (with immutable strings) is even less useful.
I'm just thinking of allocating all the space I need for the array and not having to increase on the run. Allows the code to be neater and me not to have to worry about things going out of bounds
@if_zero_equals_one: (1) It won't go out of bounds because you won't access it by indices, you will iterate it. (2) Lists are over-allocating anyway (appending is amortized O(1)!) and [item]*n won't be significantly faster than that (especially considering that both will only take a tiny fraction of overall runtime if you do anything interesting at all). Doubly so if you're going to replace most of the items anyway later on.
3

The best and most convenient method for creating a string array in python is with the help of NumPy library.

Example:

import numpy as np
arr = np.chararray((rows, columns))

This will create an array having all the entries as empty strings. You can then initialize the array using either indexing or slicing.

Comments

2
strlist =[{}]*10
strlist[0] = set()
strlist[0].add("Beef")
strlist[0].add("Fish")
strlist[1] = {"Apple", "Banana"}
strlist[1].add("Cherry")
print(strlist[0])
print(strlist[1])
print(strlist[2])
print("Array size:", len(strlist))
print(strlist)

3 Comments

strlist is an array of sets, which can hold many strings. You need to initiate a set before using methods available in sets. Try the above code to see how the construct works.
You may want to add explain.
This worked for me; but I could use an explanation of how the first line works: strlist =[{}]*10. It appears the *10 is the array length and if I don't include it then my array length is 1, not zero. But does the curly braces in the array declaration mean an array-list of any type, or any length?
1

Are you trying to do something like this?

>>> strs = [s.strip('\(\)') for s in ['some\\', '(list)', 'of', 'strings']]
>>> strs 
['some', 'list', 'of', 'strings']

1 Comment

No it's more like throw things into an array and then trim it read for the next one
1

But what is a reason to use fixed size? There is no actual need in python to use fixed size arrays(lists) so you always have ability to increase it's size using append, extend or decrease using pop, or at least you can use slicing.

x = [''  for x in xrange(10)]

1 Comment

the question asks for array, answer is as pointless as it can get, even the advice of using a list
0

The error message says it all: strs[sum-1] is a tuple, not a string. If you show more of your code someone will probably be able to help you. Without that we can only guess.

1 Comment

It's all good it was user error my bad. Being new to Python didn't help
0

Sometimes I need a empty char array. You cannot do "np.empty(size)" because error will be reported if you fill in char later. Then I usually do something quite clumsy but it is still one way to do it:

# Suppose you want a size N char array
charlist = [' ']*N # other preset character is fine as well, like 'x'
chararray = np.array(charlist)
# Then you change the content of the array
chararray[somecondition1] = 'a'
chararray[somecondition2] = 'b'

The bad part of this is that your array has default values (if you forget to change them).

Comments

0
def _remove_regex(input_text, regex_pattern):
    findregs = re.finditer(regex_pattern, input_text) 
    for i in findregs: 
        input_text = re.sub(i.group().strip(), '', input_text)
    return input_text

regex_pattern = r"\buntil\b|\bcan\b|\bboat\b"
_remove_regex("row and row and row your boat until you can row no more", regex_pattern)

\w means that it matches word characters, a|b means match either a or b, \b represents a word boundary

Comments

0

If you want to take input from user here is the code

If each string is given in new line:

strs = [input() for i in range(size)]

If the strings are separated by spaces:

strs = list(input().split())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.