Posts: 3
Threads: 1
Joined: Aug 2024
Newbie here, just trying to figure some things out. every time I search on how to do something, i try it and it doesnt work. I dont get it. anyway, i have a string, x = "a,b,c,d,e,f" that I convert to a list;
a = x.split(",") gives ['a','b','c','d','e','f'] #a,b,c,d,e,d represent strings that have length longer than 1
this works just fine and as expected. but what I cant figure out is how to extract any one of these elements into a string so I can split that.
online search says I should be able to do this;
y = a[1] #which i expect to put the 2nd element of the list (b) into a string, but this doesnt compile.
i can extract the 2nd element with
y=a[:2]
y=y[1:]
but this appears to be still considered a list. when I do a len on it, it is of size 1, the size of the list, not the size of the string, which I know to be four. and i can not do a split on it which is what I want to do
p = y.split("x") #p will have a length of at least 4, possibly more, depends on each line in the file I read in.
Posts: 3
Threads: 1
Joined: Aug 2024
addendum: the following kind of works
p = str(y)
however, this appears to be "['0x04']" and not '0x04' which is what I expect.
the length of this string is 10, not 4, and somehow converting the list element to a string included ' quotes and brackets. and i thought that in python '=", but apparently not.
so when i do a split on this, I get
["[' 0", "04 ']"] #!!!!!!!!!!!
so python is keeping quotes and brackets during these conversions for some reason.
Posts: 1,583
Threads: 3
Joined: Mar 2020
(Aug-23-2024, 04:16 AM)alexs Wrote: y = a[1] #which i expect to put the 2nd element of the list (b) into a string, but this doesnt compile.
You need to give more details and show exactly what it says. Your code looks just fine to this point.
>>> x = "a,b,c,d,e,f"
>>> a = x.split(",")
>>> y = a[1]
>>> y
'b' Show the exact code and the exact output.
Posts: 6,799
Threads: 20
Joined: Feb 2020
You get a list for y = a[:2] because :2 is Python syntax for a "slice". From the documentation
https://docs.python.org/3/glossary.html#term-slice
Quote:slice¶
An object usually containing a portion of a sequence. A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5]. The bracket (subscript) notation uses slice objects internally.
A slice can also be created using the built-in slice function.
https://docs.python.org/3/library/functions.html#slice
Quote:class slice(stop)
class slice(start, stop, step=None)
Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None.
start
stop
step
Slice objects have read-only data attributes start, stop, and step which merely return the argument values (or their default). They have no other explicit functionality; however, they are used by NumPy and other third-party packages.
Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.
An example of slicing. This is the first time I've used the slice function instead of the ":" syntax.
x = "a,b,c,d,e,f"
a = x.split(",")
print(a)
s = slice(2)
print(s, a[s], a[:2]) Output: ['a', 'b', 'c', 'd', 'e', 'f']
slice(None, 2, None) ['a', 'b'] ['a', 'b']
Note that the brackets [] around ['a', 'b', 'c', 'd', 'e', 'f'] and ['a', 'b'] are only artifacts of printing a list, as are the quotes around 'a'. These are visual cues that python uses to provide extra information about the object being printed. Python uses [] to tell us that it is printing a list, and '' to tell us that the elements in the list are str objects.
Posts: 6,799
Threads: 20
Joined: Feb 2020
I think you might have some confusion caused by the unavoidable difference between objects and their representation when displayed. For example, you cannot remove the brackets around a list. A list does not have any brackets to remove. However, you can display a list without displaying the brackets.
x = "1,2,3"
a = x.split(',')
print(a)
print(*a)
print(a[0], a[1], a[2]) Output: ['1', '2', '3']
1 2 3
print(a) prints the list a, and since a is a list, it prints the [] to tell us it is printing a list, and it prints the '' to tell us the contents are str objects and not ints. print(*a) unpacks the list, replacing the list object with the list's contents. It is the same as printing a[0], a[1], a[2]. The values in "a" are unchanged by the printing and are the same each time they are printed. Any visual difference results from how python represents the objects when printing.
So back to your question about 0x04. I assume you have something like this:
x = "0x01,0x02,0x03,0x04"
a = x.split(',')
print(a) Output: ['0x01', '0x02', '0x03', '0x04']
And now you are trying to get rid of the "0x"?
First you must think about what you want. Are you trying to convert 0x04 to an int? If so, use the int() function to do the conversion.
x = "0x01,0x02,0x03,0x04"
a = []
for hex_str in x.split(','):
a.append(int(hex_str, base=16))
print(a, sum(a)) Output: [1, 2, 3, 4] 10
Posts: 3
Threads: 1
Joined: Aug 2024
Thanks for your help, I understand this a lot better now. Turns out there was a strange error in my code I tracked down that was causing my code to no compile when I try to extract an element out of the list. This error is a result of not having to declare variables, any other language would have failed on this line, but instead if failed on a different line, misdirecting me.
|