Trying to create an array. Keep getting TypeError
I'm trying to get a better grasp on the data structures.
I've been trying to add an array as an element of another array, but i keep getting the TypeError: Array item must be unicode character, when I try to create an array. I'm following videos/everything I read to a T from what i can tell.
from array import array
Swords = array('u',['Steel Sword', 'Bronze Sword', 'Iron Sword'])
Axes = ['Steel Axe', 'Bronze Axe', 'Iron Axe']
Maces = ['Steel Mace','Bronze Mace','Iron Mace']
Bows = ['Wood Bow', 'Bone Bow', 'Obsidian Bow']
Daggers = ['Steel Dagger', 'Bronze Dagger', 'Obsidian Dagger']
Weapons = array('u',([Swords])
for i in Weapons:
print(i)
Any idea what is going on?
arrays python-3.x typeerror
add a comment |
I'm trying to get a better grasp on the data structures.
I've been trying to add an array as an element of another array, but i keep getting the TypeError: Array item must be unicode character, when I try to create an array. I'm following videos/everything I read to a T from what i can tell.
from array import array
Swords = array('u',['Steel Sword', 'Bronze Sword', 'Iron Sword'])
Axes = ['Steel Axe', 'Bronze Axe', 'Iron Axe']
Maces = ['Steel Mace','Bronze Mace','Iron Mace']
Bows = ['Wood Bow', 'Bone Bow', 'Obsidian Bow']
Daggers = ['Steel Dagger', 'Bronze Dagger', 'Obsidian Dagger']
Weapons = array('u',([Swords])
for i in Weapons:
print(i)
Any idea what is going on?
arrays python-3.x typeerror
The'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use thearray
datatype instead of a normallist
?
– jk622
Nov 21 '18 at 1:13
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
In general, you should be using alist
. Unless you are trying to use a particular function available to thearray
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.
– jk622
Nov 21 '18 at 1:56
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59
add a comment |
I'm trying to get a better grasp on the data structures.
I've been trying to add an array as an element of another array, but i keep getting the TypeError: Array item must be unicode character, when I try to create an array. I'm following videos/everything I read to a T from what i can tell.
from array import array
Swords = array('u',['Steel Sword', 'Bronze Sword', 'Iron Sword'])
Axes = ['Steel Axe', 'Bronze Axe', 'Iron Axe']
Maces = ['Steel Mace','Bronze Mace','Iron Mace']
Bows = ['Wood Bow', 'Bone Bow', 'Obsidian Bow']
Daggers = ['Steel Dagger', 'Bronze Dagger', 'Obsidian Dagger']
Weapons = array('u',([Swords])
for i in Weapons:
print(i)
Any idea what is going on?
arrays python-3.x typeerror
I'm trying to get a better grasp on the data structures.
I've been trying to add an array as an element of another array, but i keep getting the TypeError: Array item must be unicode character, when I try to create an array. I'm following videos/everything I read to a T from what i can tell.
from array import array
Swords = array('u',['Steel Sword', 'Bronze Sword', 'Iron Sword'])
Axes = ['Steel Axe', 'Bronze Axe', 'Iron Axe']
Maces = ['Steel Mace','Bronze Mace','Iron Mace']
Bows = ['Wood Bow', 'Bone Bow', 'Obsidian Bow']
Daggers = ['Steel Dagger', 'Bronze Dagger', 'Obsidian Dagger']
Weapons = array('u',([Swords])
for i in Weapons:
print(i)
Any idea what is going on?
arrays python-3.x typeerror
arrays python-3.x typeerror
asked Nov 20 '18 at 23:39
Adag89Adag89
708
708
The'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use thearray
datatype instead of a normallist
?
– jk622
Nov 21 '18 at 1:13
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
In general, you should be using alist
. Unless you are trying to use a particular function available to thearray
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.
– jk622
Nov 21 '18 at 1:56
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59
add a comment |
The'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use thearray
datatype instead of a normallist
?
– jk622
Nov 21 '18 at 1:13
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
In general, you should be using alist
. Unless you are trying to use a particular function available to thearray
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.
– jk622
Nov 21 '18 at 1:56
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59
The
'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use the array
datatype instead of a normal list
?– jk622
Nov 21 '18 at 1:13
The
'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use the array
datatype instead of a normal list
?– jk622
Nov 21 '18 at 1:13
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
In general, you should be using a
list
. Unless you are trying to use a particular function available to the array
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.– jk622
Nov 21 '18 at 1:56
In general, you should be using a
list
. Unless you are trying to use a particular function available to the array
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.– jk622
Nov 21 '18 at 1:56
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59
add a comment |
1 Answer
1
active
oldest
votes
The 'u'
type code corresponds to Python’s obsolete unicode character. This means it will work with unicode characters. You can test this
test_one = array("u", ["u2641","u2642","u2643"])
for i in test_one:
print(i)
You can also see it with this
test_two = array("u", ["T","e","s","t"])
for i in test_two:
print(i)
Notice, in both cases it is a single character. Not entire strings. In order to do the string, you would have to convert each string to a list of characters.
test_three = array("u", [ch for ch in "Test"])
for i in test_three:
print(i)
Lastly, if you want to break down the individual characters from a list of strings you can do a list comprehension similar to test_three
or you can use a generator.
def character_generator(word_list):
for word in word_list:
for ch in word:
yield ch
test_four = array("u", character_generator(["Test","One","Two"]))
for i in test_four:
print(i)
At the end of the day though, the u
typecode is for individual characters. Not strings.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403248%2ftrying-to-create-an-array-keep-getting-typeerror%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The 'u'
type code corresponds to Python’s obsolete unicode character. This means it will work with unicode characters. You can test this
test_one = array("u", ["u2641","u2642","u2643"])
for i in test_one:
print(i)
You can also see it with this
test_two = array("u", ["T","e","s","t"])
for i in test_two:
print(i)
Notice, in both cases it is a single character. Not entire strings. In order to do the string, you would have to convert each string to a list of characters.
test_three = array("u", [ch for ch in "Test"])
for i in test_three:
print(i)
Lastly, if you want to break down the individual characters from a list of strings you can do a list comprehension similar to test_three
or you can use a generator.
def character_generator(word_list):
for word in word_list:
for ch in word:
yield ch
test_four = array("u", character_generator(["Test","One","Two"]))
for i in test_four:
print(i)
At the end of the day though, the u
typecode is for individual characters. Not strings.
add a comment |
The 'u'
type code corresponds to Python’s obsolete unicode character. This means it will work with unicode characters. You can test this
test_one = array("u", ["u2641","u2642","u2643"])
for i in test_one:
print(i)
You can also see it with this
test_two = array("u", ["T","e","s","t"])
for i in test_two:
print(i)
Notice, in both cases it is a single character. Not entire strings. In order to do the string, you would have to convert each string to a list of characters.
test_three = array("u", [ch for ch in "Test"])
for i in test_three:
print(i)
Lastly, if you want to break down the individual characters from a list of strings you can do a list comprehension similar to test_three
or you can use a generator.
def character_generator(word_list):
for word in word_list:
for ch in word:
yield ch
test_four = array("u", character_generator(["Test","One","Two"]))
for i in test_four:
print(i)
At the end of the day though, the u
typecode is for individual characters. Not strings.
add a comment |
The 'u'
type code corresponds to Python’s obsolete unicode character. This means it will work with unicode characters. You can test this
test_one = array("u", ["u2641","u2642","u2643"])
for i in test_one:
print(i)
You can also see it with this
test_two = array("u", ["T","e","s","t"])
for i in test_two:
print(i)
Notice, in both cases it is a single character. Not entire strings. In order to do the string, you would have to convert each string to a list of characters.
test_three = array("u", [ch for ch in "Test"])
for i in test_three:
print(i)
Lastly, if you want to break down the individual characters from a list of strings you can do a list comprehension similar to test_three
or you can use a generator.
def character_generator(word_list):
for word in word_list:
for ch in word:
yield ch
test_four = array("u", character_generator(["Test","One","Two"]))
for i in test_four:
print(i)
At the end of the day though, the u
typecode is for individual characters. Not strings.
The 'u'
type code corresponds to Python’s obsolete unicode character. This means it will work with unicode characters. You can test this
test_one = array("u", ["u2641","u2642","u2643"])
for i in test_one:
print(i)
You can also see it with this
test_two = array("u", ["T","e","s","t"])
for i in test_two:
print(i)
Notice, in both cases it is a single character. Not entire strings. In order to do the string, you would have to convert each string to a list of characters.
test_three = array("u", [ch for ch in "Test"])
for i in test_three:
print(i)
Lastly, if you want to break down the individual characters from a list of strings you can do a list comprehension similar to test_three
or you can use a generator.
def character_generator(word_list):
for word in word_list:
for ch in word:
yield ch
test_four = array("u", character_generator(["Test","One","Two"]))
for i in test_four:
print(i)
At the end of the day though, the u
typecode is for individual characters. Not strings.
edited Nov 21 '18 at 3:15
answered Nov 21 '18 at 2:20
jk622jk622
3211312
3211312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403248%2ftrying-to-create-an-array-keep-getting-typeerror%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The
'u'
type code corresponds to Python’s obsolete unicode character. Why exactly are you trying to use thearray
datatype instead of a normallist
?– jk622
Nov 21 '18 at 1:13
@jk622 I was just trying to get comfortable working with arrays. Is there any difference between an array, and a list in python, or should I just use a list when I need an array?
– Adag89
Nov 21 '18 at 1:48
In general, you should be using a
list
. Unless you are trying to use a particular function available to thearray
class, all you'd be doing is adding a constraint to the datatype of the objects inside it.– jk622
Nov 21 '18 at 1:56
Ok, I did some additional reading, and makes sense when to use which. Why the type error for trying to place a list inside of an array though? This is what every tutorial I have watched has shown.
– Adag89
Nov 21 '18 at 1:59